Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Point of intersection

Status
Not open for further replies.

SteveD73

Programmer
Jan 5, 2001
109
GB
Hi

Given 4 sided polygon defined by four points:

CPoint p1
CPoint p2
CPoint p3
CPoint p4

How can I calculate the point of intersection between the diaganols (p1,p3) and (p2,p4)?

Thanks for your help.
 
rectangle is:

p1 p2

p3 p4


Intersection of diagonals:
x coordinate = p3.x + ((p4.x - p3.x)/2)
y coordinate = p3.y + ((p1.y - p3.y)/2)

x is horizontal axis
y is vertical axis

p1.x is the x coordinate of p1
p1.y is the y coordinate of p1

etc.

This seems right doesn't it?
 
The above only works when the shape is a rectangle. I need a formulae that will work with any 4 sided shape.
 
it is a mathematical formula that you need. First you need to put the lines into slope intercept form (y=mx+b)

lets say a simple square
p1(0,0)
p2(0,4)
p3(4,4)
p4(4,0)

and we draw lines from p1 to p3 and from p2 to p4

slope(m) = rise/run so the slope of the line from p1 to p3 is 1 rise = 4 run = 4

and 2 to 4 is -1 rise = -4 run = 4

now we have formulas for the lines
y = x + 0
y = -x + 4

we know the y coordinates will be the same so we can:

x+0 = -x+4

solve for x and we get 2x = 4 or x = 2

2 is the x intercept of the 2 lines
substitute 2 into an original equation

y = 2 + 0
y = -2 +4

either one give interseckiton of (2,2)

Matt

Sorry if this was already posted, i was away from my desk for a bit

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top