Click here to Skip to main content
15,741,818 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
I have two point (x1,y1) and (x2,y2) which makes a straight line. Now I want to draw two lines through the terminal points which will be perpendicular to the main line.

in attached image you can see what i need.

Pointsfind.png - Google Drive[^]

if anyone know how to do please help
Thanks in Advance.
Updated 30-Dec-15 2:02am
Richard MacCutchan 30-Dec-15 5:03am    
You would do it by writing some code. Your link does not seem to contain any useful information, please edit your question and explaion in better detail.
[no name] 30-Dec-15 9:13am    
A vector prependicular to (a,b) is (-b,a). This one can prove by the help of scalar product.
In your case (a,b)= (x2,y2)-(x1,y1).
Now with this (-b,a) vector one can easily calculate the four (?,?) vectors respectively their representants.
Sergey Alexandrovich Kryukov 30-Dec-15 13:10pm    
The problem is ambiguous, because you can draw multiple perpendicular lines.
[no name] 30-Dec-15 13:44pm    
Nothing is ambiguous, except the abs() of the perpendicular vectors.
Sergey Alexandrovich Kryukov 30-Dec-15 15:46pm    
You are right: I noticed that the inquirer needs two lines "through the terminal points". Of course, this is solvable. And "abs of perpendicular vector" also does not make things ambiguous, because the question is not about the vector. It's enough to point out two points per straight line, "representative enough"; ideally, the points at the interception of these lines to the viewport boundaries (or beyond this boundary).

A vector perpendicular to (a,b) is (-b,a). This one can prove by the help of scalar product (also named dot product).

In your case (a,b)= (x2,y2)-(x1,y1). Now, with this (-b,a) vector one can easily calculate the four (?,?) vectors respectively their “representants” (sorry I do not find the right word in English).

Because (?,?) is not specified more precise, I personally would use the above mentioned calculations. Another way is to use “unit vector” for (-b,a), but in case of graphics one has to pay attention, not to lose precisions.

Sorry for my bad English, I hope it helps.

It is simple vector computation. Even if others here think it is more.
Share this answer
Sergey Alexandrovich Kryukov 30-Dec-15 12:52pm    
What are you talking about?

How (a, b) and (a, -b) can be perpendicular? Can't you see that (100, 1) and (100, -1) are positioned at sharp angle?

"Find a perpendicular line" is ambiguous. The question is not about finding a perpendicular vector, it's about finding two points. You can draw multiple perpendicular lines...

[no name] 30-Dec-15 12:57pm    
Sorry, I forgot to mention the trivial Solutions as an "exception"... but I think OP Needs first to consume the non trivial ones :-)

It is up to you to describe it more precisely.

This comment is not longer valid after edit of SA's comment
Sergey Alexandrovich Kryukov 30-Dec-15 12:59pm    
Sorry, I fixed my comment above. The whole problem is posed ambiguously, and your post is plain wrong.
Please read again.
[no name] 30-Dec-15 13:19pm    
Please explain "and your post is plain wrong". Thank you.
Sergey Alexandrovich Kryukov 30-Dec-15 15:41pm    
My mistake, sorry. I'll try to fix it.
Thanks to all for your reply.
i did the same by using the following code

dx = x1-x2
dy = y1-y2
dist = sqrt(dx*dx + dy*dy)
dx /= dist
dy /= dist
x3 = x1 + (N/2)*dy // where N is the length of line to be draw.
y3 = y1 - (N/2)*dx
x4 = x1 - (N/2)*dy
y4 = y1 + (N/2)*dx

and same way i found the x5,y5 and x6 y6.
Share this answer

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