Math help. Rotating points

Community Forums/General Help/Math help. Rotating points

RifRaf(Posted 2013) [#1]
I am attempting to check if a point is in a rectangle ( 4 points)
All the data I have to work with is

1.the rectangles original 4 points
2.The current scale
3. the current roll or 2d rotation

I can offset my inside check for scale but am not sure how to calculate for the rotation, can anyone help me out ?

;The 4 corners calculated from x, to x+ width, and y to y+ width
x1=quad\X-(quad\W*.5)
x2=quad\X+(quad\w*.5)
Y2=quad\Y-(quad\H*.5)
y2=quad\Y+(quad\h*.5)

;adjust corner checks for scale
x1=x1/(.75+(squad\scalex*.25))
x2=x2*(.75+(squad\scalex*.25))
y1=y1/(.75+(squad\scaley*.25))
y2=y2*(.75+(squad\scaley*.25))

;adjust corner checks for z rotation
;this trips me up a bit.. any help ?
;obviously what I have below here does not work
	x1=x1*Cos(quad\roll) 
	x2=x2*Cos(quad\roll) 
	y1=y1*Cos(quad\roll) 
	y2=y2*Cos(squad\roll)

checkpoint(Pointx,pointy,x1,y1,x2,y2)




GfK(Posted 2013) [#2]
Found this in the code archives: http://www.blitzbasic.com/codearcs/codearcs.php?code=2037

Should do the job, no?


big10p(Posted 2013) [#3]
Yep, most things of this order are in the arcs. I just noticed I have this one of my own. Forgot all about it:
http://www.blitzbasic.com/codearcs/codearcs.php?code=2137


RifRaf(Posted 2013) [#4]
It does a good point in poly, but I cant see where it rotates points in space.. which is what I need to do. Take four 2d points.. and apply a rotation to them from their center of mass.

Then I can check for my point in poly


Kryzon(Posted 2013) [#5]
You don't need to scale separately. Scaling will directly influence the final width and height of the rectangle, so you can calculate the points of the scaled rectangle like this:

;Calculate the scaled half width and half height of the rectangle.

Local halfWidth# = ( ( quad\w * quad\scaleX ) / 2.0 )
Local halfHeight# = ( ( quad\h * quad\scaleY ) / 2.0 )

;Rotate and add the scaled displacement to the quad's center.
;The scaled dimensions were not rotated in the above calculation because they'll 
;be used further along in this unrotated state.

x1 = quad\x - ( halfWidth * Cos( quad\roll ) )
y1 = quad\y - ( halfHeight * Sin( quad\roll ) )

x2 = quad\x + ( halfWidth * Cos( quad\roll ) )
y2 = quad\y + ( halfHeight * Sin( quad\roll ) )

;[...]

;To check the intersection, transform the point to be tested from the
;rotated rectangle's space to screen space (aligned to the screen)
;and test against the unrotated rectangle's hafWidth and halfHeight.
;Note the previously calculated points of the rectangle aren't used, so 
;if you don't have any use for them you can ignore their calculations above.

;Calculate the point to be tested as a vector with the quad's center as origin.

Local resetAngle# = -quad\roll
Local tformPointX# = ( pointX - quad\x ) * Cos( resetAngle )
Local tformPointY# = ( pointY - quad\y ) * Sin( resetAngle )

;If the (absolute) vector components of the distance betwen the test point and the center of 
;the quad are less than the quad's half width and less than half its height, then they're intersecting.

If ( Abs( tformPointX ) < halfWidth ) And ( Abs( tformPointY ) < halfHeight ) Then
	
	;Intersection detected between the point and the rotated, scaled rectangle.

EndIf
I haven't tested this.


RifRaf(Posted 2013) [#6]
Thanks Kryzon,

I'll try it.