Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
Hello!

As this is a math related problem, and I am really bad at math I ask it from you, so there is a line (0,0) and (0,2).
Now I would like to get all the points within this line.
How could I do this?
Posted
Comments
Anthony Mushrow 11-Jul-11 13:36pm    
There are an infinite number of points on a line so you're going to need to clarify what points you're actually after. Do you mean points on a grid that the line passes directly through?
velvet7 11-Jul-11 13:40pm    
Yes. So the points would be:(0,0),(0,1),(0,2)
Christian Graus 11-Jul-11 20:23pm    
Oh, come on. He's clearly wanting the points that can be plotted in code, and either way, the equation for getting the points would work for any point inbetween.

Find the slope m=(x1-x2)/(y1-y1) and make a loop of x where y=mx+c (c=0). now get
(x,y) for a range of x. I'm not sure about my equations. please check those are correct or not.


Correct equations, given (x1, y1) (x2, y2):

m = (y1 - y2) / (x1-x2);
c = y1 - x1 * m;

Then, for any given x:

y = mx + c;
 
Share this answer
 
v3
Comments
Christian Graus 11-Jul-11 20:23pm    
Correct. It's worth adding, the 0 for c is because x is initially zero, so if the first point was 3,0, then c would be 3. I can't vote, sorry, or I'd 5 this.
Аslam Iqbal 12-Jul-11 5:12am    
I can't remember, maybe you are right. I just tried to give him concept.
velvet7 12-Jul-11 4:10am    
Thank you!
Have a look at this: http://en.wikipedia.org/wiki/Line_drawing_algorithm[^].
It may not be exactly what you are after, but it leads to versions which your PC uses every day!
 
Share this answer
 
Since you didn't specify the domain let me assure you this will be impossible to do since there is an infinite number of points between any two points. If you expected a different answer from this one it may be rooted in the question you posed.

Cheers!

—MRB
 
Share this answer
 
Comments
velvet7 11-Jul-11 13:41pm    
Please check out my comment above.
Quote:
Find the slope m=(x1-x2)/(y1-y1) and make a loop of x where y=mx+c (c=0). now get
(x,y) for a range of x. I'm not sure about my equations. please check those are correct or not.


Correct equations, given (x1, y1) (x2, y2):

m = (y1 - y2) / (x1-x2);
c = y1 - x1 * m;

Then, for any given x:

y = mx + c;


The above answer works fine for non vertical lines where X stays constant.
If X stays constant as in a straight vertical line, e.g. at every point on the line X = 2 or 3 ....., the above equation will not work.

Also, between any two points on any type of line, there are an infinite amount of points. Therefore, you would have to specify some type of period either on x or y, e.g. loop get every point at x\y = 0.000001 to 666.00001.
 
Share this answer
 
Comments
Ralf Meier 3-Jan-18 6:36am    
Did you realized that this question is nearly 6.5 years old ...
I think this Solution is no more helpful ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900