I'm stuck on a simple math question. Getting a rectangle's end points after rotating. (sin/cos?)

Hello,

   I’m almost sure what I’m doing is correct on paper but I just can’t get it to work on corona. I have a rectangle:

temprect = display.newRect(500, 500, 100, 10)

 What I want to do is to find out the X and Y values where rectangle ends. In this case it is X: 550, Y: 500

 But I want to be able to find them even after rotation so I’m using:

tempX = temprect.x + ((temprect.width) \* math.cos(temprect.rotation)) tempY = temprect.y + ((temprect.width) \* math.sin(temprect.rotation))

But it doesn’t give me the correct X, Y. I also tried using:

local bounds = temprect.contentBounds tempX = bounds.xMax tempY = bounds.yMax

But still no chance. The code works for angles 0, 90, 180 but nothing else. I must be missing something obvious and simple. Any help? :slight_smile:

Hi.

math.cos and math.sin take radians, not degrees, so you’d actually want to pass either math.rad(temprect.rotation) or temprect.rotation * (math.pi / 180).

That said, what you have probably isn’t what you want. In general, it will be the corners that poke out; the sides only happen to give the right answers when they line up with two corners, as in the 0, 90, 180 cases you mention.

I think you’d want something like (partially tested):

temprect.rotation = some\_angle -- Convert the corner displacements to content coordinates local x1, y1 = temprect:localToContent(-50, -5) local x2, y2 = temprect:localToContent(-50, 5) local x3, y3 = temprect:localToContent(50, -5) lcoal x4, y4 = temprect:localToContent(50, 5) local maxx = math.max(x1, x2, x3, x4) local maxy = math.max(y1, y2, y3, y4) -- To agree with contentBounds... local tempX = math.floor(maxx) local tempY = math.floor(maxy)

To do it manually, instead of going through localToContent , you’d do

  x1 = temprect.x + math.cos(angle) * x - math.sin(angle) * y

  y1 = temprect.y + math.sin(angle) * x + math.cos(angle) * y

 – x2 = …

 – y2 = …

 – etc.

where x and y are the corner displacements.

Ahh it was 1 hour of frustration, was so painful. I read the documents for everything, even math.sin() but I can’t believe I missed the ‘the angle in radians’ part. The results were so weird…

Thanks a lot!

Hi.

math.cos and math.sin take radians, not degrees, so you’d actually want to pass either math.rad(temprect.rotation) or temprect.rotation * (math.pi / 180).

That said, what you have probably isn’t what you want. In general, it will be the corners that poke out; the sides only happen to give the right answers when they line up with two corners, as in the 0, 90, 180 cases you mention.

I think you’d want something like (partially tested):

temprect.rotation = some\_angle -- Convert the corner displacements to content coordinates local x1, y1 = temprect:localToContent(-50, -5) local x2, y2 = temprect:localToContent(-50, 5) local x3, y3 = temprect:localToContent(50, -5) lcoal x4, y4 = temprect:localToContent(50, 5) local maxx = math.max(x1, x2, x3, x4) local maxy = math.max(y1, y2, y3, y4) -- To agree with contentBounds... local tempX = math.floor(maxx) local tempY = math.floor(maxy)

To do it manually, instead of going through localToContent , you’d do

  x1 = temprect.x + math.cos(angle) * x - math.sin(angle) * y

  y1 = temprect.y + math.sin(angle) * x + math.cos(angle) * y

 – x2 = …

 – y2 = …

 – etc.

where x and y are the corner displacements.

Ahh it was 1 hour of frustration, was so painful. I read the documents for everything, even math.sin() but I can’t believe I missed the ‘the angle in radians’ part. The results were so weird…

Thanks a lot!