Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Rotation
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Nablo




PostPosted: Mon Dec 12, 2011 6:52 pm   Post subject: Rotation

What is it you are trying to achieve?

I am trying to rotate a box 90 degrees in turing with its axis of rotation in the center of the box. I am also trying to rotate a few lines continuously (sort of like a car wheel spinning) with its axis of rotation being the center of the oval on which the lines are situated on.


What is the problem you are having?

I do not know how I am to apply sin, cosin, tan, or even if I should use them (I know grade 10 math) to achieve this 3D rotation on the shapes.


Describe what you have tried to solve this problem

I've tried looking through the Turing Index, several submission on compsci as well as any tutorials that were avaliable on compsci but I unfortunately have yet to see the solution to my specific problem.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

I only have a picture of the objects which I am trying to rotate.


EDIT

I figured out how to rotate the rectangular shape 90 degrees. Although, I am still wondering how to do rotate the spikey shape 1 degree at a time, I'm thinking for loop + trig but I don't have enough knowledge to go from there...

Please specify what version of Turing you are using
Turing V. 4.1



Level 1.bmp
 Description:
I am trying to rotate the spikey figures in a car like motion and the robot 90 degrees at a time.
 Filesize:  576.05 KB
 Viewed:  162 Time(s)

Level 1.bmp


Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Mon Dec 12, 2011 7:55 pm   Post subject: RE:Rotation

Look up Pic.Rotate(). It's exactly what you need.
Nablo




PostPosted: Mon Dec 12, 2011 8:08 pm   Post subject: Re: Rotation

I already read up the pic rotate module. The thing is that I am trying to rotate a turing generated shape rather than a picture.
Insectoid




PostPosted: Mon Dec 12, 2011 8:45 pm   Post subject: RE:Rotation

Oh, then you need to use math! You can't really rotate a rectangle, or any pre-defined shape. If you want to rotate it, you need to draw all the individual lines for each item and draw.fill() the insides. To rotate, you apply rotation math to each vertex in your shape with a function (you'll have to write it yourself). Google probably has information about this, but I warn you it might be a bit advanced.
Nablo




PostPosted: Mon Dec 12, 2011 8:59 pm   Post subject: Re: Rotation

What is rotation math, and as you can see, my pictures might be too complex for them to be constructed by lines.
Dreadnought




PostPosted: Tue Dec 13, 2011 2:42 pm   Post subject: Re: Rotation

I see your "spikey things" are kinda round. Depending on what you're actually trying to do with them, you might be able to make this work with pic.rotate after all and save yourself a hassle.

Check this out!
Turing:

View.Set ("offscreenonly")
var pic1_Array : array 1 .. 36 of int
var pic2_Array : array 1 .. 36 of int

% Make picture 1
Draw.FillMapleLeaf (110, 110, 190, 190, 12)
pic1_Array (36) := Pic.New (100, 100, 200, 200)
cls
% Make picture 2
Draw.FillStar (110, 110, 190, 190, 12)
pic2_Array (36) := Pic.New (100, 100, 200, 200)
% Note that when I make a picture, I make it bigger than necessary
%      to make sure the edges don't get cut off by pic.rotate

% Generate the rotations ofthe picture (here I make a picture for every 10 degrees
for i : 1 .. 35
    pic1_Array (i) := Pic.Rotate (pic1_Array (36), 10 * i, 50, 50)
    pic2_Array (i) := Pic.Rotate (pic2_Array (36), 360 - (10 * i), 50, 50)
end for

% Now the fun part!
for j : 1 .. 15
    % This is a coloured background, to show that you can still draw these pictures over other graphics
    if j ~= 12 then % my pictures are red (12) so I skip this colour
        Draw.FillBox (0, 0, maxx, maxy, j)
        View.Update
        for i : 1 .. 36
            Draw.FillBox (0, 0, maxx, maxy, j)
            % Draw the pictures using picMerge (you could use picCopy if your background was plain white)
            Pic.Draw (pic1_Array (i), 100, 100, picMerge)
            Pic.Draw (pic2_Array (i), 300, 100, picMerge)
            View.UpdateArea (100, 100, 200, 200)
            View.UpdateArea (300, 100, 400, 200)
            Time.DelaySinceLast (40)
        end for
    end if
end for

It could work for a rectangle too, but you might need to make big picture for it to not cut off edges (you can figure out how big using the Pythagorean theorem).
RandomLetters




PostPosted: Tue Dec 13, 2011 6:09 pm   Post subject: RE:Rotation

From http://en.wikipedia.org/wiki/Transformation_matrix

"For rotation by an angle θ counter clockwise about the origin, the functional form is x' = xcos θ − ysin θ and y' = xsin θ + ycos θ"

It's easy to see how this works if you look at rotating points on a unit circle.

However, this only rotates point around the origin (0,0).

For objects, you will need to translate all the coordinates so that the axis of rotation is at the origin (i.e. translate them by (-px, -py)). Then, rotate them around the "origin". And then translate them back onto the screen coordinates.
Raknarg




PostPosted: Tue Dec 13, 2011 9:37 pm   Post subject: RE:Rotation

Turing:

View.Set ("offscreenonly, graphics:700;700")

const xPos := 350
const yPos := 350
var x, y, b : int := 0 % variables for the Mouse.Where
var t : real % Theta, or the angle

loop
    Mouse.Where (x, y, b)
    if x = xPos and y > yPos then
        t := 270
    elsif x < xPos and y = yPos then
        t := 0
    elsif x = xPos and y < yPos then
        t := 90
    elsif x > xPos and y = yPos then
        t := 180
    elsif x = xPos and y = yPos then
        t := 0
    elsif x > xPos then
        t := arctand ((y - yPos) / (x - xPos)) + 180
    else
        t := arctand ((y - yPos) / (x - xPos))
    end if
    % This thing here calculates your angle. It'll make sense if you've ever done vectors before. It uses the function tan-1. The ones before it are for mathematical correctness; you cannot divide anything by zero.

    Draw.ThickLine (xPos, yPos, xPos - round (40 * cosd (t)), yPos - round (40 * sind (t)), 4, 7) % This is important for drawing shapes according to an angle. You add onto the original postion. In the case of x, its the radius * cos(angle). For y it's radius * sin (angle)
    View.Update
    cls
end loop


It's pretty simple. This is exacrly what randomletters was talking about. You can use this idea to rotate an object. Basically what you are doing is changing the x and y properties according to the angle given. You can imagine it as a right-angled triangle: In this triangle, you have the x (adjacent), the y (opposite) and your hypotenuse (distance a certain line covers). Lets go over some math:

How do you calculate the opposite if you have an angle and a hypotenuse? Like so:
opp (y) = hyp * sin (angle)

How do you calculate the adjacent if you have an angle and a hypotenuse? Like so:
adj (x) = hyp * cos (angle)

Now you have useful information. Instead of the length of the line, you now have the change in x and the change in y for this line, which you can use in drawing a line.

I probably explained this poorly, so if it's still confusing, the internet is a great place to look.
Sponsor
Sponsor
Sponsor
sponsor
Velocity




PostPosted: Wed Dec 14, 2011 8:45 pm   Post subject: RE:Rotation

@ raknarg do you know the formula for cod and sin and tan or something? you some sort of super genius? how do you implement it in a long line that so easily :OOOOO
Raknarg




PostPosted: Thu Dec 15, 2011 2:56 pm   Post subject: RE:Rotation

Yeah, it's basic trig:

sin (theta) = opposite/hypotenuse
cos (theta) = adjacent/hypotenuse

Or do you mean the formula to for cos and sin themselves? no.
smool




PostPosted: Thu Dec 15, 2011 5:15 pm   Post subject: RE:Rotation

alternatly, if you wanted to rotate not to a set angle but rather rotate it by x degree more than what you already have:

X' = Xcos(theta) - Ysin(theta)
Y' = Xsin(theta) + Ycos(theta)

That rotates around coordinate (0, 0), so you would have to translate everything, rotate, then translate again. You might want to have a procedure. Your code might look like this:

Turing:

View.Set ("offscreenonly")
var x : array 1..4 of int := init (100, 100, 300, 300)
var y : array 1..4 of int := init (100, 300, 300, 100)
var mx, my, mxpast, bnum : int := 0

proc Rotate (var x_, y_ : int, xmid, ymid, theta : int)
    var x, y : int
    x_ -= xmid
    y_ -= ymid
    x := round (x_ * cosd (theta) - y_ * sind (theta))
    y := round (x_ * sind (theta) + y_ * cosd (theta))
    x_ := x + xmid
    y_ := y + ymid
end Rotate

loop
    mousewhere (mx, my, bnum)
    for a : 1..4
        Rotate (x (a), y (a), 200, 200, mx - mxpast)
    end for
    Draw.FillPolygon (x, y, 4, 42)
    View.Update
    delay (10)
    cls
    mxpast := mx
end loop
Raknarg




PostPosted: Fri Dec 16, 2011 11:47 am   Post subject: RE:Rotation

Note that in that procedure, the rotation is inverted when your my is above 200. Don't know if thatwas intentional.
Nablo




PostPosted: Sun Dec 18, 2011 10:38 am   Post subject: Re: Rotation

Thanks a lot guys! I really appreciate all of your help! Just another thing, I tried using the transformation Matrix but as one of you guys already said, it rotates the object around the origin rather than a point set by you. Raknarg and smool, I havn't tried using that approach but I will! Promise to send you guys the program once I am done with it! Dreadnough, I checked out your program and it is quite interesting! I'm still trying to understand all of your programs but hopefully I'll get through the mind boggling stage and understand the. Again, thanks a lot for your help!

Ps. What does -= or += mean?
Aange10




PostPosted: Sun Dec 18, 2011 12:06 pm   Post subject: RE:Rotation

Turing:

var n : int

n += 1

% is the same as

n := n + 1

% Likewise

n -= 1

% Is the same as

n := n - 1
Insectoid




PostPosted: Sun Dec 18, 2011 1:01 pm   Post subject: RE:Rotation

In case you're wondering, /=, *=, and I think even div= and mod= are valid (I don't have Turing available to test div/mod though).
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 18 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: