# Yay! Nearly finished intersection method

Thankyou very much for all of the help on the forums.

I have finally got my rectangle intersection method to work to some extent. I got it to detect intersection. I believe everything is complete apart from taking the points.

I got it to work great for non-rotated rectangles. But now I need to get this method to support rotated rectangles so how do I do this? The code only handles part of the intersection method but everything else should work fine with rotation (though bare in mind I have not tested with rotated items yet). Basically what the method does is grab each vertices of a rectangle. The comments describe each vertices position eg. ‘top left’, ‘top right’. But the points will not be plotted correctly if the rectangle is rotated so how would I go about doing this?

``````Function GetPoint(extends shape as RectShape, id as integer) As Point
dim x, y, width, height as integer
x = shape.x
y = shape.y
width = shape.width
height = shape.height

dim p as Point

//get point from rectangle shape
select case id
case 0
//top left
p = new Point(x, y)
case 1
//top right
p = new Point(x + width, y)
case 2
//bottom right
p = new Point(x + width, y + height)
case 3
//bottom left
p = new Point(x, y + height)
end select

p.move(-(width / 2), -(height / 2))
return p
End Function``````

Thanks a ton. The help on this forum has been tremendous.

A rotated rectangle is just a polygon, which is why such is usually handled with normal polygon intersection algorithm which then works for all polygons.

I would google Polygon Intersection (maybe add C# or .NET) to the search param since then you usually get easier to read code than what you would get if getting it in some lower level language.

[quote=161822:@Björn Eiríksson]A rotated rectangle is just a polygon, which is why such is usually handled with normal polygon intersection algorithm which then works for all polygons.

I would google Polygon Intersection (maybe add C# or .NET) to the search param since then you usually get easier to read code than what you would get if getting it in some lower level language.[/quote]
Thanks for the advice but I have already decided how to work with the intersection. The method I posted is not even an issue with intersection. Its a method that I am using for intersection but it could be used for other things.

The information which I am specifically looking for however it simply plotting grabbing the vertices of each point of a rectangle.

Do you think this might be what I am looking for?
http://stackoverflow.com/questions/1469149/calculating-vertices-of-a-rotated-rectangle

For that then your just rotating a point around point of origin.

So if you know how to rotate a point around point of origin then you know how to rotate any polygon around point of origin since what you do is you just run the algorithm on all points in the polygon (in your case a rectangle), and have same point of origin for all your polygon points. (corners of your rectangle)

Point of origin is like what point you want your rectangle to rotate around, can be any of its corner, its centre or even a point outside the rectangle.

Here is how to calculate rotating one point around point of origin: (which you would then just apply 4 times for a rectangle, sending always in same point of origin for all 4 corners)

void PointF_Rotate(REALobject instance,float angle,REALobject originPoint)
{
if (!originPoint) return;

``````ClassData(PointFClass, instance, PointFData, me);
ClassData(PointFClass, originPoint,PointFData,origin);

angle = angle * 3.14159265358979323846264338327950288 / 180;

me->x = (origin->x + ((me->x - origin->x) * cos(angle) - (me->y - origin->y) * sin(angle)));
me->y = (origin->y + ((me->x - origin->x) * sin(angle) + (me->y - origin->y) * cos(angle)));
``````

}

[quote=161832:@Björn Eiríksson]For that then your just rotating a point around point of origin.

So if you know how to rotate a point around point of origin then you know how to rotate any polygon around point of origin since what you do is you just run the algorithm on all points in the polygon (in your case a rectangle), and have same point of origin for all your polygon points. (corners of your rectangle)

Point of origin is like what point you want your rectangle to rotate around, can be any of its corner, its centre or even a point outside the rectangle.

Here is how to calculate rotating one point around point of origin: (which you would then just apply 4 times for a rectangle, sending always in same point of origin for all 4 corners)

void PointF_Rotate(REALobject instance,float angle,REALobject originPoint)
{
if (!originPoint) return;

``````ClassData(PointFClass, instance, PointFData, me);
ClassData(PointFClass, originPoint,PointFData,origin);

angle = angle * 3.14159265358979323846264338327950288 / 180;

me->x = (origin->x + ((me->x - origin->x) * cos(angle) - (me->y - origin->y) * sin(angle)));
me->y = (origin->y + ((me->x - origin->x) * sin(angle) + (me->y - origin->y) * cos(angle)));
``````

}[/quote]
Thankyou very much. I will give this a go.

[quote=161832:@Björn Eiríksson]For that then your just rotating a point around point of origin.

So if you know how to rotate a point around point of origin then you know how to rotate any polygon around point of origin since what you do is you just run the algorithm on all points in the polygon (in your case a rectangle), and have same point of origin for all your polygon points. (corners of your rectangle)

Point of origin is like what point you want your rectangle to rotate around, can be any of its corner, its centre or even a point outside the rectangle.

Here is how to calculate rotating one point around point of origin: (which you would then just apply 4 times for a rectangle, sending always in same point of origin for all 4 corners)

void PointF_Rotate(REALobject instance,float angle,REALobject originPoint)
{
if (!originPoint) return;

``````ClassData(PointFClass, instance, PointFData, me);
ClassData(PointFClass, originPoint,PointFData,origin);

angle = angle * 3.14159265358979323846264338327950288 / 180;

me->x = (origin->x + ((me->x - origin->x) * cos(angle) - (me->y - origin->y) * sin(angle)));
me->y = (origin->y + ((me->x - origin->x) * sin(angle) + (me->y - origin->y) * cos(angle)));
``````

}[/quote]
I don’t understand why you have given me C++ code though. That’s what it looks like to me anyway.

It looks like I might be able to use something from this thread.
http://forums.realsoftware.com/viewtopic.php?f=1&t=8431

Thanks for all the help

Because I am c++ developer ? and thats the code I had ?

The math works the same you know.

[quote=161887:@Björn Eiríksson]Because I am c++ developer ? and thats the code I had ?

The math works the same you know.[/quote]
I presume this is C++ plugin?

I know the maths works the same.

Thanks

Yes thats from the TypeLibF plugin the PointF.Rotate method

Oh right. Thanks