Archived

This topic is now archived and is closed to further replies.

Lord Hen

Lines

Recommended Posts

Lord Hen    122
If i have a line, defined with X1,X2,Y1,Y2...is there any mathematical formula to find other points on the line, say, 10 points equal distances apart in the line? Thanks Alcohol & calculus dont mix. Never drink & derive.

Share this post


Link to post
Share on other sites
sBibi    241
you need a parametric representation of your line...
something like:

x = x1 + t * (x2 - x1)
y = y1 + t * (y2 - y1)

and to get your 10 points, you just have to make a loop like this:

x2 -= x1;
y2 -= y1;
for (i = 0.0f; i < 1.0f; i += 0.1f)
{
p->x = x1 + i * x2;
(p++)->y = y1 + i * y2;
}

Share this post


Link to post
Share on other sites
Another solution is to find the length of the line and the unit vector in the direction of your line, aka pointing from x1, y1 to x2, y2. Then you divide your the lenght by the number of points you need and multipy the result by your unit vector. Then its simply a matter of putting the vectors together, starting at the point x1, y1.

Share this post


Link to post
Share on other sites
haro    502

#define HOPS 10.0

double deltaX = (p2x - p1x)/HOPS;
double deltaY = (p2y - p1y)/HOPS;

double newX, newY;
newX = p1x;
newY = p1y;

for(int i = 1; i <= HOPS; i++)
{
newX = p1x + deltaX * i;
newY = p1y + deltaY * i;
}


Why nobody else gives a plain english code dump is beyond me. Outside of that though, I agree with sBibi. Parametrics are much easier and quicker for just about everything, to work with.

[edited by - haro on July 10, 2003 5:03:51 AM]

Share this post


Link to post
Share on other sites
haro    502
Needless to say, newX/newY would be reals. So add 0.5 and cast them to integers and you would have rounded screen coordinates.

Share this post


Link to post
Share on other sites
sBibi    241
rounded screen coordinates?
yes, and loose any kind of subpixel accuracy... :/

StormArmageddon> the method you describe is just another way to see a parametric representation

Share this post


Link to post
Share on other sites
haro    502
quote:
Original post by sBibi
rounded screen coordinates?
yes, and loose any kind of subpixel accuracy... :/



Bah, writing a blender takes about 2 seconds and isn''t even really required for decent looks.

Share this post


Link to post
Share on other sites