We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › How to Calculate a point on a line...
Pages: 1 2 
How to? Calculate a point on a line... (Read 3544 times)
How to? Calculate a point on a line...
Dec 11th, 2009, 7:13am
 
Hi all,

I would like to know how can i calculate a point on a line.

For example, I have a "line (0,0,10,10);" what i must to do so i can know any point of that line? Logically i know that the points are 1,1 | 2,2 | 3,3 and so on... But there is any expression that let me do this automatically?


If there is another answered question about this on the forum i'm sorry, but i didn't find it...

Hope you can help me guys! Smiley Thnx
Re: How to? Calculate a point on a line...
Reply #1 - Dec 11th, 2009, 7:39am
 
This might help (though note that there's now a PVector object in Processing - so no need to use 'provector'):
http://processing.org/hacks/hacks:detecting-line-to-line-intersection
Re: How to? Calculate a point on a line...
Reply #2 - Dec 11th, 2009, 7:50am
 
you can easily use lerp() to do that...

http://processing.org/reference/lerp_.html

Re: How to? Calculate a point on a line...
Reply #3 - Dec 11th, 2009, 8:10am
 
Or just use
y = a * x + b
...
Re: How to? Calculate a point on a line...
Reply #4 - Dec 11th, 2009, 10:36am
 
PhiLho  wrote on Dec 11th, 2009, 8:10am:
Or just use
y = a * x + b
...


can you show me an example with line coordenates
And the meaning of the expression
Re: How to? Calculate a point on a line...
Reply #5 - Dec 11th, 2009, 12:21pm
 
Of course!  I'm over-complicating things as usual: you're just interested in a point being on a straight line.  And a straight line can be defined by: y = a*x + b; so you just have to find the values that define your line and test the current coordinates with the formula.
Re: How to? Calculate a point on a line...
Reply #6 - Dec 11th, 2009, 2:25pm
 
Example: line(5, 10, 30, 50)
We have:
10 = a * 5 + b
50 = a * 30 + b

Subtraction: 40 = a * 25 so a = 40/25 = 1.6
So b = 10 - 5 * 1.6 = 2

Implementation:
Code:
int x1 = 5, y1 = 10;
int x2 = 30, y2 = 50;
float a, b;
float x, y, px, py;
float step = 0.1;

void setup()
{
size(100, 100);
a = y2 - y1;
if (x1 == x2)
{
a = 0;
}
else
{
a /= x2 - x1;
}
b = y1 - x1 * a;
px = x1; py = y1;
}

void draw()
{
x = px + step;
if (x > x2) noLoop();
y = fun(x);
line(px, py, x, y);
px = x; py = y;
}

float fun(float x)
{
return a * x + b;
}
Re: How to? Calculate a point on a line...
Reply #7 - Dec 11th, 2009, 3:57pm
 
Just to make 5 posts... (sorry guys)
Re: How to? Calculate a point on a line...
Reply #8 - Dec 11th, 2009, 3:58pm
 
Just to make 5 posts... (sorry guys)
Re: How to? Calculate a point on a line...
Reply #9 - Dec 11th, 2009, 3:58pm
 
Just to make 5 posts... (sorry guys)
Re: How to? Calculate a point on a line...
Reply #10 - Dec 11th, 2009, 3:58pm
 
PhiLho  wrote on Dec 11th, 2009, 2:25pm:
Example: line(5, 10, 30, 50)
We have:
10 = a * 5 + b
50 = a * 30 + b

Subtraction: 40 = a * 25 so a = 40/25 = 1.6
So b = 10 - 5 * 1.6 = 2



Ok i tryed to implement it without success, i will post a picture of what i'm doing...

...

So i draw random lines, and when i call a function it will draw objects along the line in a random point of that line.
Imagine drawing ellipses where the x, y position are the image p1, p2.

Can someone help me coding the function with those expression Smiley

Cheers
Re: How to? Calculate a point on a line...
Reply #11 - Dec 12th, 2009, 2:11am
 
GonGaxPT wrote on Dec 11th, 2009, 3:58pm:
Ok i tryed to implement it without success

What kind of failure
Have you looked at my sample implementation
Re: How to? Calculate a point on a line...
Reply #12 - Dec 12th, 2009, 9:58am
 
I'm not sure if this is what you need but I needed something similar the other day. After several hours of searching I found this and it was exactly what I was looking for.

Quote:
X = point1.x + (point2.x - point1.x) * Ratio
Y = point1.y + (point2.y - point1.y) * Ratio


If Ratio is at 0, it will give you x1 any y1. If Ratio is 1 it will give you x2 and y2. Anything between 0 and 1 will give you an x and y co-ordinate on that line that's appropriate to the ratio along it. You might be able to adapt this if it isn't what you wanted.
Re: How to? Calculate a point on a line...
Reply #13 - Dec 12th, 2009, 10:03am
 
Biscuiting wrote on Dec 12th, 2009, 9:58am:
I'm not sure if this is what you need but I needed something similar the other day. After several hours of searching I found this and it was exactly what I was looking for.


X = point1.x + (point2.x - point1.x) * Ratio
Y = point1.y + (point2.y - point1.y) * Ratio


I will give it a try! Smiley Tnx
Re: How to? Calculate a point on a line...
Reply #14 - Dec 12th, 2009, 10:04am
 
PhiLho  wrote on Dec 12th, 2009, 2:11am:
GonGaxPT wrote on Dec 11th, 2009, 3:58pm:
Ok i tryed to implement it without success

What kind of failure
Have you looked at my sample implementation


O didn't understand how to get the x and y of the new point, i suck at math... :S Sorry...
Pages: 1 2