Computer Science Canada

Trig origin

Author:  Degensquared [ Fri Oct 26, 2007 10:43 am ]
Post subject:  Trig origin

I have just created a program that allows the user to rotate a box, w, (drawn with 4 lines) using trig, but it only rotates from the top left hand corner, I know how to make it rotate from another corner, but I'm not sure how to change the origin that it rotates around to the center, not one of the points for the lines.

code:

setscreen ("graphics:800;600,offscreenonly")
Mouse.ButtonChoose ("multibutton")
var angle, x, y, b, anglev : int := 0
var left, middle, right : int
var linex : array 1 .. 3 of int
var liney : array 1 .. 3 of int
anglev := 1
loop
    Mouse.Where (x, y, b)
    left := b mod 10
    middle := (b - left) mod 100
    right := b - middle - left
    if left = 1 then
        angle += anglev
    end if
    if right = 100 then
        angle -= anglev
    end if
    if angle >= 360 then
        angle := anglev
    end if
    linex (1) := x
    liney (1) := y
    linex (2) := linex (1) - 25 + round (cosd (angle - 90) * 50)
    liney (2) := liney (1) + 25 + round (sind (angle - 90) * 50)
    linex (3) := linex (1) - 25 + round (cosd (angle) * 50)
    liney (3) := liney (1) + 25 + round (sind (angle) * 50)
    drawline (linex (1) - 25, liney (1) + 25, linex (1) - 25 + round (cosd (angle) * 50), liney (1) + 25 + round (sind (angle) * 50), black)
    drawline (x - 25, y + 25, x - 25 + round (cosd (angle - 90) * 50), y + 25 + round (sind (angle - 90) * 50), black)
    drawline (linex (2), liney (2), linex (2) + round (cosd (angle) * 50), liney (2) + round (sind (angle) * 50), black)
    drawline (linex (3), liney (3), linex (3) + round (cosd (angle - 90) * 50), liney (3) + round (sind (angle - 90) * 50), black)
    Time.DelaySinceLast (2)
    View.Update
    cls
end loop


Author:  richcash [ Fri Oct 26, 2007 11:56 pm ]
Post subject:  Re: Trig origin

Are you having trouble with finding out the coordinates of the center of a rectangle or are you having trouble with rotating a box around a point (which is basically the same as rotating 4 points around a point of rotation)?

Do the following for all four points and join the new points to get the new box.

1) Find the angle of the line joining each point and the point of rotation by using arctan
code:
angle(1) = arctand ((cy - y(1)) / (cx - x(1)))
if angle(1) < 0 then angle(1) += 180
if y(2) < y(1) then angle(1) += 180
where cx and cy are the coordinates of your point of rotation and x(1) and y(1) is your first point from the box

2) Find the distance between each point and the point of rotation by using pythagorean theorem :
code:
dist(1) = ((cx - x(1))^2 + (cy - y(1))^2)^0.5

3)To rotate a point by n degrees, use :
code:
new_x(1) = cosd (angle(1) + n) * dist(1) + cx
new_y(1) = sind (angle(1) + n) * dist(1) + cy


*The above is pseudocode.


: