| sargon94 (259) | ||||
|
Many people know what a line is, and many more can draw them using slope. But I've noticed that when trying to draw a line using only the origin and end points, people stumble and fall. The question is quite simple: How can I draw something without a concrete x and y coordinate? The answer is also simple: You find them. Let us imagine you wish to move an object from the origin point (0,0) to a certain end point (8,20). Well, a human would simply realize that (8, 20) translates into a slope of 2/5 (2 up, 5 right) and draw the line accordingly. Unfortunatly, a computer doesn't possess a ruler with which to draw the line. So the person is forced to tell the computer every single point the line goes through. This is done quite easily. In this example, we are moving up and over. So, we already have the Y coordinates. They are points 1 through 20. But how to get X? It's easy, X is a function of Y. Like so: X=(X1/Y1)*Y To get a better idea: X= X1 * Y __Y1 In this equation, X1 is 8 (the target point's X coordinate) and Y1 is 20 (the target point's Y coordinate). As for Y, simply substitute 1 through 20 to get the X coordinates for it. The program sample would look like this:
Which creates the following output:
| ||||
|
Last edited on
|
||||
| hamsterman (4327) | |||
|
And when Y1 == 0... ? To be honest I'm not sure what are you trying to do. It looks like you want to get the coordinates of pixels on the line, but then x and y should be ints. If you want to do that, the right way would be to take the longer of the two sides. That way there is no division by 0 and no gaps in the line.
| |||
|
|
|||
| hanst99 (2845) | |
| Actually, why don't you juse use a linear interpolation here? That's high school level math, easy to use, doesn't need any more code, and doesn't require divisions. Oh, and of course you also don't need arrays for it. | |
|
|
|
| hamsterman (4327) | |
|
This already is linear interpolation (between points 0; 0 and x1; y1) Divisions are inevitable. Array is optional. | |
|
|
|
| Disch (8347) | |
|
I just noticed the OP i using 1-based indexes and therefore is overflowing his array. Shame on him! | |
|
|
|
| moorecm (1802) | |
| Way back in the DOS days, you might find yourself writing an algorithm to draw a line. These days, not so much. It's interesting, though. If I recall, most implementations of this I've seen separate the various cases (horizontal, positive slope, negative slope, vertical). | |
|
|
|
| hanst99 (2845) | ||||||
Pseudocode:
- of course k must be between 0 and 1 here. The only necessary division is to calculate k for any specific point (e.g. if you wanted k for pixels). Then the division would be done in the client code, where the caller has the necessary control to ensure a division by 0 wouldn't occur ex for pixels:
I have full control over here. Unlike in the OPs code, a division by 0 will never occur here and there is no array necessary (if the user wanted one, he could simply make one). It's also not limited to a line from the origin point. | ||||||
|
Last edited on
|
||||||
| ultifinitus (1283) | |
|
I believe that in this case the line drawing is used to calculate the position of the bullets in his game... If I were to do the same thing, I would just calculate the necessary velocity and apply that to my object. Barring friction, it would work perfectly. That way you wouldn't have to calculate every single position, just simply add your velocity to your current position, I would also base both off of a float or double, rather than truncating to an int value. | |
|
|
|
| hanst99 (2845) | |
| I think it was actually about drawing a line here, I doubt anyone is unable to move an object along a line. | |
|
|
|
| ultifinitus (1283) | |||
Lol, I don't know hanst =) | |||
|
|
|||
| hanst99 (2845) | ||
Right above that,
| ||
|
|
||
| ultifinitus (1283) | |
|
That's completely true, I can see your perspective, how about we ask him? @sargon: answer? | |
|
Last edited on
|
|
| ne555 (4041) | |
|
No divisions, just integer addition, subtraction and bit shifting http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm | |
|
|
|