-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|447|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- Socoder -> Cobra -> Another rotation maths question Posted : Wednesday, 27 January 2010, 03:46 HoboBen 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 --v -=-=- Posted : Wednesday, 27 January 2010, 03:49 Jayenkai 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 HoboBen 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 HoboBen 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 Jayenkai 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 HoboBen Code's just a bit sloppy as I'm prototyping... Going to change a lot of my integers to reals. -=-=-