### Another rotation maths question

UserMessage
Posted : Wednesday, 27 January 2010, 03:46 | Permalink
HoboBen

WW Entries : 9
 I'm using this code to transform points of a vector graphic by offset, origin, scale and rotation. It's mostly working except when I change the origin of rotation - a change in X origin works, but a change in Y origin doesn't quite (see image). I can't figure out a way to change my code so that it works for a change in Y origin without then messing up the result when I have a change in X origin. Any ideas? Thanks very much. Function TransformedX(svg:^svgs, pt: ^svgPoints, zoom: Real=1.0, offsetx: Integer=0) : Integer ; export Var x: Integer theta, r, rotation, sign: Real Begin r = Sqrt( ((pt.x - svg.xorigin)^2) + ((pt.y - svg.yorigin)^2) ) If (ToReal(pt.x - svg.xorigin) <> 0.0) then theta = Atan(ToReal(pt.y - svg.yorigin) / ToReal(pt.x - svg.xorigin)) Else theta = Atan(ToReal(pt.y - svg.yorigin)) Endif sign = Sgn(theta); If sign = 0 then sign = 1 rotation = svg.angle x = r * Cos(theta + rotation) result = ToReal( ((x * svg.scale * sign) + (svg.xoffset + offsetx)) * zoom) End Function TransformedY(svg:^svgs, pt: ^svgPoints, zoom: Real=1.0, offsety: Integer=0) : Integer ; export Var y: Integer theta, r, rotation, sign: Real Begin r = Sqrt( ((pt.x - svg.xorigin)^2) + ((pt.y - svg.yorigin)^2) ) If (ToReal(pt.x - svg.xorigin) <> 0.0) then theta = Atan(ToReal(pt.y - svg.yorigin) / ToReal(pt.x - svg.xorigin)) Else theta = Atan(ToReal(pt.y - svg.yorigin)) Endif sign = Sgn(theta); If sign = 0 then sign = 1 rotation = svg.angle y = r * Sin(theta + rotation) result = ToReal( ((y * svg.scale * sign) + (svg.yoffset + offsety)) * zoom) End --> -----
Posted : Wednesday, 27 January 2010, 03:49 | Permalink
Jayenkai

WW Entries : 106
 I've just scanned it, not properly read what you're doing, but "If (ToReal(pt.x - svg.xorigin) <> 0.0) then" seems to be sticking out a bit. |edit| Upon a proper reading, never mind.. |edit|-----''Load, Next List!''
Posted : Wednesday, 27 January 2010, 03:52 | Permalink
HoboBen

WW Entries : 9
 That's just to avoid a divide-by-zero error on the next line. (edit - though technically the else case should be Atan(INFINITY) instead - I've changed that now.)-----
Posted : Wednesday, 27 January 2010, 04:01 | Permalink
HoboBen

WW Entries : 9
 Hahahaha, I've got it!!! This always happens - I work on things for hours and get nowhere, then after posting here for help I solve it it minutes! Had to add this after sign = Sgn(theta)... If ToReal(pt.y - svg.yorigin) < 0.0 then theta = theta + 180 That's all there was to it. Wooo! edit - In TransformX to fix an occasional problem around y=0, I needed: If (pt.y - svg.yorigin) < (-0.1 * Sgn(pt.x - svg.xorigin)) then theta = theta + 180.0-----
Posted : Wednesday, 27 January 2010, 04:09 | Permalink
Jayenkai

WW Entries : 106
 Hmm.. Seems a little odd, but ok! Also, still not sure why you're doing Result=ToReal() when the function's listed as integer...-----''Load, Next List!''
Posted : Wednesday, 27 January 2010, 04:16 | Permalink
HoboBen

WW Entries : 9
 Code's just a bit sloppy as I'm prototyping... Going to change a lot of my integers to reals.-----
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 670|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
 Latest PostsFavourite Amstrad CPC Games steve_ancell Sat 21:02 Shoutbox Topic - 319 steve_ancell Sat 20:52 Inside Donald Trump rockford Sat 12:09 Any Feature Requests? Jayenkai Sat 09:36 More Monkey Madness - With Ducks rockford Sat 05:13 Arms on Switch rockford Sat 05:02 Time for a new Android Test Doohickey Jayenkai Fri 09:26 CSS-Me-Do - SoCoder2 Jayenkai Fri 06:28 Pillowcases for kids! rockford Thu 16:15 Manchester Arena Explosion Jayenkai Thu 02:04 More

 Latest Items Showcase : Hiveszzoom Fri 16:10 Dev-Diary : My Journey into NES Developmentrychan Thu 12:31 Showcase : FlappadiddleJayenkai Sun 14:39 Snippet : QFindJayenkai Sun 13:02 Showcase : Tiny BlocksJayenkai Sun 04:08 Showcase : Read Error Arychan Fri 05:13 Blog : All my makes!Jayenkai Tue 05:48 Showcase : Infinitronrychan Mon 18:03 Showcase : Quadobanrskgames Fri 10:11 Blog : My Arduino experience.steve_ancell Wed 17:02 Showcase : Roguelike ExplorerPakz Fri 06:59 News : Newsletter #311Jayenkai Thu 17:27 Link : Super Shapes Exploration KitAndy_A Thu 11:09 Dev-Diary : Sensitive - Arduboy!rychan Thu 17:27 Snippet : Skylinessteve_ancell Tue 14:25More

Who's Online
steve_ancell
Sat, at 21:32
Sat, at 18:50
therevillsgames
Sat, at 18:00
rockford
Sat, at 16:19
shroom_monk
Sat, at 16:02
Jayenkai
Sat, at 15:42
spinal
Sat, at 12:08
rychan
Sat, at 08:56
rskgames
Sat, at 07:21
Pakz
Sat, at 04:16