• ### What is your GameDev Story?

#### Archived

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

# Math problems

This topic is 6774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys, I know that this s a stupid question and that I studied these stuff when I was at school but I really need your supper and help. I have a problem: I have a segment from P1 and P2 and I would like to draw a little segment from the middle of the segment that is perpendicular to it. So I have to find two point: the middle point of the segment that is (P1.X + P2.X)/2 and (P1.Y + P2.Y)/2 and the other point that is along the perpendicular of the segment. I really don''t know how to find it. Please help

##### Share on other sites
hi,

you can use the fact that the dot product of 2 perpendicular vectors is 0

let p4 the point in the middle of segment p1p2 and p3 the point you are looking for:

p1p2.p3p4=0

((p2.x-p1.x)*(p4.x-p3.x))+((p2.y-p1.y)*(p4.y-p3.y))=0

fix a value to p3.x and that''s it!

lunasol

##### Share on other sites
In 2D you can rotate a vector by 90 degrees with the following formula:

x'' = -y
y'' = x

Now take your line segment (P2-P1), rotate it 90 degrees and then scale it to the length you want it to be.

- WitchLord

##### Share on other sites
Thanks a lot guys!!!!

You are blood fo me

##### Share on other sites
Just a question Witchlord...

It works fine but obviously the perpendiculat has the same lenght of the line that I draw. How can I scale it to the lenght I want it to be ?

##### Share on other sites
to scale a normal vector:

nx is your normal x value
ny is your normal y value
nz is your normal z value
scale is the length you want it to be

length = scale / sqrt(nx*nx+ny*ny+nz*nz);
nx *= length;
ny *= length;
nz *= length;

for 2d you can cut alot of this out:

length = scale / sqrt(nx*nx+ny*ny);
nx *= length;
ny *= length;

"Like all good things, it starts with a monkey.."

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634056
• Total Posts
3015282
×