# RectShape Intersects Method???

QUESTION IN SHORT:
‘Intersects’ method that extends RectShape with rotation support???

Hey there,

How could I create an ‘Intersects’ method that works just like REALbasic.Rect’s but works for RectShape with rotation.

The only feature missing which I really need from the Intersects method that REALbasic.Rect’s does not have is calculations for rotated rectangles so I need something which works for that. I been looking all over the web and I am really stuck on how to do this.

Could somebody please help me get started or even write there own method for this if it is convenient enough?

When I go searching for guides I find it quite difficult to understand the maths jargon.

Thanks

"Given TWO quadrilateral shapes in 2D space where each shape is rotated around its own center by a given angle, where the angle of each may be different and from 0 to 360 degrees, determine
if the vector representing and of the four sides of rectangle “A” bisect any of the four sides of rectangle “B”

If so, one brute force method would be a 2d LINE intersection algorithm

Assuming rectangle “A” has corners at Ax(0),Ay(0) thru Ax(3),Ay(3) and rectangle “B” is Bx(0),By(0) thru Bx(3),By(3)

``````they_intersect=false
for i=0 to 3
for j=0 to 3
if intersect(ax(i),ay(i),bx(j),by(j)) then
they_intersect=true
exit for i
end if
next j
next i``````

Not fast, but it should give you an idea… This works regardless of the shape or rotation angle (and will work for shapes with ANY number of sides (just change “3”)

Oh yeah… and its up to you to “google” “LINE INTERSECTION IN 2D SPACE”

[quote=155258:@Dave S]are you asking for this?

"Given TWO quadrilateral shapes in 2D space where each shape is rotated around its own center by a given angle, where the angle of each may be different and from 0 to 360 degrees, determine
if the vector representing and of the four sides of rectangle “A” bisect any of the four sides of rectangle “B”

If so, one brute force method would be a 2d LINE intersection algorithm

Assuming rectangle “A” has corners at Ax(0),Ay(0) thru Ax(3),Ay(3) and rectangle “B” is Bx(0),By(0) thru Bx(3),By(3)

``````they_intersect=false
for i=0 to 3
for j=0 to 3
if intersect(ax(i),ay(i),bx(j),by(j)) then
they_intersect=true
exit for i
end if
next j
next i``````

Not fast, but it should give you an idea… This works regardless of the shape or rotation angle (and will work for shapes with ANY number of sides (just change “3”)

Oh yeah… and its up to you to “google” “LINE INTERSECTION IN 2D SPACE”[/quote]
Is brute force the most efficient way to do this? I will give it a try to see if it is efficient enough.

I will go on another exploration on Google.

Thanks

Now I see what you are doing…i think. So your code shows me how to detect collision for a quadratic shape but I have to Google line collision to create the method for line intersection. Okay, thanks.

That is exactly correct

[quote=155373:@Dave S]ut I have to Google line collision to create the method for line intersection. Okay, thanks.
That is exactly correct[/quote]
Thanks. Will go for it then.

[quote=155258:@Dave S]are you asking for this?

"Given TWO quadrilateral shapes in 2D space where each shape is rotated around its own center by a given angle, where the angle of each may be different and from 0 to 360 degrees, determine
if the vector representing and of the four sides of rectangle “A” bisect any of the four sides of rectangle “B”

If so, one brute force method would be a 2d LINE intersection algorithm

Assuming rectangle “A” has corners at Ax(0),Ay(0) thru Ax(3),Ay(3) and rectangle “B” is Bx(0),By(0) thru Bx(3),By(3)

``````they_intersect=false
for i=0 to 3
for j=0 to 3
if intersect(ax(i),ay(i),bx(j),by(j)) then
they_intersect=true
exit for i
end if
next j
next i``````

Not fast, but it should give you an idea… This works regardless of the shape or rotation angle (and will work for shapes with ANY number of sides (just change “3”)

Oh yeah… and its up to you to “google” “LINE INTERSECTION IN 2D SPACE”[/quote]
But what the shape is small and completely overlaps the shape and therefore is not touching the edges.

then you need to determine any one corner point is inside … if at least ONE point is inside they overlap to some degree

I don’t understand. :\

Thanks

If at least one corner of one of the rectangles is inside the other rectangle then the rectangles intersect. Look back at your other thread about this and find the project that I posted about this, or google how to determine if a point is inside of a rectangle. Its not hard…

Ah…that’s clear now. At least one corner will be contained within another. I could use the contains function for that, I believe. Thanks.

I am understanding the rest of it and I can think of how I am going to do the main bit but I am finding it hard to find a working solution for what I want. I have tried porting a GameMaker script to Xojo and I have even created a project for testing my line algorithm. Could someone help me please get the ‘Intersects’ method to work within the LineShape class I made. The project comes with everything needed to test the line intersection, it just needs the line intersection method to work.

The textfields to the left are for determining the first line and the textfields to the right are for the second. It’s just a prototype so I can get things working and test if it actually is the line intersection method that is wrong.

https://www.dropbox.com/s/crh295jhjwiaafz/DEBUG.xojo_binary_project?dl=0

Thanks

Your lack of comments or descriptive names and references to what you created your code from make it difficult to determine what is wrong. A method from one of my projects might help you:

[code]Function Intersects(A1 as xojo.Core.Point, A2 as xojo.Core.Point, B1 as xojo.Core.Point, B2 as xojo.Core.Point, byref intersectionPoint as xojo.Core.Point) As Boolean
dim Aslope as Double = (A2.Y - A1.Y)/(A2.X - A1.X)
dim Bslope as Double = (B2.Y - B1.Y)/(B2.X - B1.X)

dim AIntercept as Double = A1.Y - AslopeA1.X
dim BIntercept as Double = B1.Y - Bslope
B1.X

if Aslope = Bslope then //parallel lines
if AIntercept <> BIntercept then
Return False
else
Return True
end if
end if

dim XIntersection as Double = (BIntercept-AIntercept)/(Aslope-Bslope)
dim YIntersection as Double = Aslope*XIntersection + AIntercept

intersectionPoint = new xojo.Core.Point(XIntersection,YIntersection)
Return True
End Function
[/code]
It returns whether or not the lines intersect, and if there is a unique intersection point (i.e. lines aren’t parallel) sets the intersectionPoint parameter to the point of intersection.

[quote=155447:@Jason King]Your lack of comments or descriptive names and references to what you created your code from make it difficult to determine what is wrong. A method from one of my projects might help you:

[code]Function Intersects(A1 as xojo.Core.Point, A2 as xojo.Core.Point, B1 as xojo.Core.Point, B2 as xojo.Core.Point, byref intersectionPoint as xojo.Core.Point) As Boolean
dim Aslope as Double = (A2.Y - A1.Y)/(A2.X - A1.X)
dim Bslope as Double = (B2.Y - B1.Y)/(B2.X - B1.X)

dim AIntercept as Double = A1.Y - AslopeA1.X
dim BIntercept as Double = B1.Y - Bslope
B1.X

if Aslope = Bslope then //parallel lines
if AIntercept <> BIntercept then
Return False
else
Return True
end if
end if

dim XIntersection as Double = (BIntercept-AIntercept)/(Aslope-Bslope)
dim YIntersection as Double = Aslope*XIntersection + AIntercept

intersectionPoint = new xojo.Core.Point(XIntersection,YIntersection)
Return True
End Function
[/code]
It returns whether or not the lines intersect, and if there is a unique intersection point (i.e. lines aren’t parallel) sets the intersectionPoint parameter to the point of intersection.

[quote=155447:@Jason King]Your lack of comments or descriptive names and references to what you created your code from make it difficult to determine what is wrong. A method from one of my projects might help you:

[code]Function Intersects(A1 as xojo.Core.Point, A2 as xojo.Core.Point, B1 as xojo.Core.Point, B2 as xojo.Core.Point, byref intersectionPoint as xojo.Core.Point) As Boolean
dim Aslope as Double = (A2.Y - A1.Y)/(A2.X - A1.X)
dim Bslope as Double = (B2.Y - B1.Y)/(B2.X - B1.X)

dim AIntercept as Double = A1.Y - AslopeA1.X
dim BIntercept as Double = B1.Y - Bslope
B1.X

if Aslope = Bslope then //parallel lines
if AIntercept <> BIntercept then
Return False
else
Return True
end if
end if

dim XIntersection as Double = (BIntercept-AIntercept)/(Aslope-Bslope)
dim YIntersection as Double = Aslope*XIntersection + AIntercept

intersectionPoint = new xojo.Core.Point(XIntersection,YIntersection)
Return True
End Function
[/code]
It returns whether or not the lines intersect, and if there is a unique intersection point (i.e. lines aren’t parallel) sets the intersectionPoint parameter to the point of intersection.
I replaced my intersects method and it still does not seem to work.

https://www.dropbox.com/s/crh295jhjwiaafz/DEBUG.xojo_binary_project?dl=0

I tried to modify it to work with my project. The intersection point always seems to be -1 and the method returns true, even when there is no intersection. Any idea why it won’t work? Thanks

Thanks

First of all, a point cannot be -1 since it has both an X and Y component. Also you understand that two coplanar lines which aren’t parallel are guaranteed to intersect at some point, right? If you only want to treat the lines as line segments then you need to examine the point of intersection to make sure it is between the two end points.

Oh, so the method does not (on its own) determine whether they intersect but rather, whether they intersect across an infinite line?

Thanks

*(on its own) does not determine whether they intersect

I will have another go at it. Thanks very much for helping.

The function never seems to return false?

Thanks