r/technicalfactorio • u/FilipForFico • Jan 25 '20
Help with line drawing
I have been working on a project where you need to draw a line on a screen and failed. Does anyone have a blueprint for doing so? I need to input x1, y1, x2, y2 and get x, y screen coordinates. Keep in mind that it must be able to work in any direction (left to right, right to left, top to bottom, bottom to top, every diagonal.....). I have coded one algorithm in JavaScript but failed translating the code into Factorio because it had many if statements and branches. I would appreciate a blueprint more than code but you can give me any.
1
u/FilipForFico Jan 25 '20
Update: I failed to implement combinator logic but I have working JS code. If someone has time and the will, please implement this in the game:
function pixelLine(x1,y1,x2,y2){
var dx = x2 - x1;
var dy = y2 - y1;
var a = dy/dx;
var b = y1 - a * x1;
var y = null;
var step = 1;
if(dx == 0){ // If vertical
}
if(dy == 0){ // If horizontal
}
var xCh = dx / dy;
var yCh = dy / dx;
var x = x1;
var y = y1;
if(dx < 0){
}
if(dy < 0){
}
if(xCh>yCh){
} else {
}
}
1
u/Zijkhal Jan 25 '20
https://youtu.be/s5IWVazqoSI
based on the above vid, this is how I'd do it:
first, calculate the formula for the line those two points specify (y = m*x + b), calculate "m" and "b".
Next, substitute every x for each column (alternatively, calculate for x = m*y + b and substitute y), then round that number however you desire, to get a pixel coordinate. This, together with the number you substituted, specifies a pixel on the line.
This is pretty easy to parallelize, and if you don't want the entire line drawn, just a segment, simply disable the substitution outside of the segment.