locked
I need to rotate shapes(rectangle) but not from it's center. RRS feed

  • Question

  • I know it has something to do with trigonometry but we are two years away to learn trigonometry in our school there is not any trigonometry chapter in our mathematics textbook. So, please guide me where to start from. I would like to know the concept of rotation than a ready to run code example.

    Thankyou. :)

     

    They say working hard is good but i say working smart is best...

    Tuesday, November 6, 2012 11:09 AM

Answers

  • But while calculation we need center coordinate of rotated rectangle.  This position depends on rectangle shape.  So most of case you need trigonometric function for these calculation.

    rotated rectangle position

    This is a sequence to get rotated rectangle positon.

    (1) original rectangle position

    (2) original rectangle center (relative from rotation center)

    (3) rotated rectangle center (relative from rotation center)

    (4) rotated rectangle position


    Nonki Takahashi



    • Edited by Nonki Takahashi Wednesday, November 7, 2012 4:20 AM
    • Marked as answer by 4mir '- Wednesday, November 7, 2012 5:06 AM
    Wednesday, November 7, 2012 2:51 AM
  • Afraid its pretty much impossible without trig and matrices and cannot really teach these in a paragraph or 2.

    This is how it could be done, I know you don't just want code, but explaining trig from scratch isn't realistic.

    But basically we are finding the distance we must move the top left corner when the shape is rotated by an angle about its centre.

    You will see the combination of Sin and Cos I use are effectively a rotation matrix - see here to get started perhaps.

    gww = GraphicsWindow.Width
    gwh = GraphicsWindow.Height
    shapewidth = 120
    shapeheight = 20
    hand = Shapes.AddRectangle(shapewidth,shapeheight)
    xc = gww/2
    yc = gwh/2
    rotation = 0
    Shapes.Move(hand,xc,yc)
    GraphicsWindow.DrawEllipse(xc,yc,2,2)
    GraphicsWindow.KeyDown = keydown

    'Blob at centre  of rotation
    GraphicsWindow.BrushColor = "Red"
    GraphicsWindow.PenWidth = 0
    centre = Shapes.AddEllipse(6,6)
    'Centre of rotation relative to shape centre
    shiftX = -shapewidth/4
    shiftY = 0
    Shapes.Move(centre,xc+shapewidth/2-3+shiftX,yc+shapeheight/2-3+shiftY)

    Sub keydown
      lastkey = GraphicsWindow.LastKey
      If lastkey = "Space" Then
        rotate()
      EndIf
    EndSub

    Sub rotate
      rotation = rotation + 5
      If rotation > 360 Then
        rotation = 0
      EndIf
      Shapes.Rotate(hand,rotation)
      move()
      ' i thought this part will leave the traces of top left corner and draw a complete circle but it didnot work :(
      x = Shapes.GetLeft(hand)
      y = Shapes.GetTop(hand)
      GraphicsWindow.SetPixel(x,y,"Black")
    EndSub

    Sub move
      rot = Math.GetRadians(rotation)
      offsetX = shiftX*Math.Cos(rot) - shiftY*Math.Sin(rot)
      offsetY = shiftX*Math.Sin(rot) + shiftY*Math.Cos(rot)
      Shapes.Move(hand,xc+shiftX-offsetX,yc+shiftY-offsetY)
    EndSub

    • Edited by litdev Tuesday, November 6, 2012 10:45 PM
    • Marked as answer by 4mir '- Wednesday, November 7, 2012 5:04 AM
    Tuesday, November 6, 2012 8:06 PM

All replies

  • Shapes.Rotate(rectangle, angle) always rotates the rectangle from it's center.

    So if you would like to rotate a rectangle at it's upper left corner, you need move the rectangle as follows after/before rotation.

    to rotate rectangle at it's corner


    Nonki Takahashi

    Tuesday, November 6, 2012 2:27 PM
  • gww = GraphicsWindow.Width
    gwh = GraphicsWindow.Height
    shapewidth = 120
    hand = Shapes.AddRectangle(shapewidth,20)
    xc = gww/2
    yc = gwh/2
    rotation = 0
    Shapes.Move(hand,xc,yc)
    GraphicsWindow.DrawEllipse(xc,yc,2,2)
    GraphicsWindow.KeyDown = keydown
    
    Sub keydown
      lastkey = GraphicsWindow.LastKey
      If lastkey = "Space" Then
        move()
        rotate()
      EndIf
    EndSub
    
    Sub rotate
      rotation = rotation + 5
      If rotation > 360 Then
        rotation = 0
        EndIf
        Shapes.Rotate(hand,rotation)
        ' i thought this part will leave the traces of top left corner and draw a complete circle but it didnot work :(
        x = Shapes.GetLeft(hand)
        y = Shapes.GetTop(hand)
        GraphicsWindow.SetPixel(x,y,"Black")
    EndSub
    Sub move
      'Simply move to center??? i am not sure
      Shapes.Move(hand,xc,yc)
      EndSub

    It did not worked :(

    A little more explanation please

    anyway Thank you so much  Nonki for reply :)


    They say working hard is good but i say working smart is best...

    Tuesday, November 6, 2012 4:19 PM
  • Afraid its pretty much impossible without trig and matrices and cannot really teach these in a paragraph or 2.

    This is how it could be done, I know you don't just want code, but explaining trig from scratch isn't realistic.

    But basically we are finding the distance we must move the top left corner when the shape is rotated by an angle about its centre.

    You will see the combination of Sin and Cos I use are effectively a rotation matrix - see here to get started perhaps.

    gww = GraphicsWindow.Width
    gwh = GraphicsWindow.Height
    shapewidth = 120
    shapeheight = 20
    hand = Shapes.AddRectangle(shapewidth,shapeheight)
    xc = gww/2
    yc = gwh/2
    rotation = 0
    Shapes.Move(hand,xc,yc)
    GraphicsWindow.DrawEllipse(xc,yc,2,2)
    GraphicsWindow.KeyDown = keydown

    'Blob at centre  of rotation
    GraphicsWindow.BrushColor = "Red"
    GraphicsWindow.PenWidth = 0
    centre = Shapes.AddEllipse(6,6)
    'Centre of rotation relative to shape centre
    shiftX = -shapewidth/4
    shiftY = 0
    Shapes.Move(centre,xc+shapewidth/2-3+shiftX,yc+shapeheight/2-3+shiftY)

    Sub keydown
      lastkey = GraphicsWindow.LastKey
      If lastkey = "Space" Then
        rotate()
      EndIf
    EndSub

    Sub rotate
      rotation = rotation + 5
      If rotation > 360 Then
        rotation = 0
      EndIf
      Shapes.Rotate(hand,rotation)
      move()
      ' i thought this part will leave the traces of top left corner and draw a complete circle but it didnot work :(
      x = Shapes.GetLeft(hand)
      y = Shapes.GetTop(hand)
      GraphicsWindow.SetPixel(x,y,"Black")
    EndSub

    Sub move
      rot = Math.GetRadians(rotation)
      offsetX = shiftX*Math.Cos(rot) - shiftY*Math.Sin(rot)
      offsetY = shiftX*Math.Sin(rot) + shiftY*Math.Cos(rot)
      Shapes.Move(hand,xc+shiftX-offsetX,yc+shiftY-offsetY)
    EndSub

    • Edited by litdev Tuesday, November 6, 2012 10:45 PM
    • Marked as answer by 4mir '- Wednesday, November 7, 2012 5:04 AM
    Tuesday, November 6, 2012 8:06 PM
  • If you don't use trigonometric function, so rotate only 0, 30, 45, 60, 90, ...[degree].

    For other angle, use trigonometric function (sin, cos).

    trigonometric function

    Be careful that trigonometric function needs [radian] but [degree] for Small Basic language.

    You can use Math.GetRadians(degree) to convert from [degree] to [radian].


    Nonki Takahashi


    Wednesday, November 7, 2012 1:06 AM
  • But while calculation we need center coordinate of rotated rectangle.  This position depends on rectangle shape.  So most of case you need trigonometric function for these calculation.

    rotated rectangle position

    This is a sequence to get rotated rectangle positon.

    (1) original rectangle position

    (2) original rectangle center (relative from rotation center)

    (3) rotated rectangle center (relative from rotation center)

    (4) rotated rectangle position


    Nonki Takahashi



    • Edited by Nonki Takahashi Wednesday, November 7, 2012 4:20 AM
    • Marked as answer by 4mir '- Wednesday, November 7, 2012 5:06 AM
    Wednesday, November 7, 2012 2:51 AM
  • Thank you Litdev sir, Nonki. It helped me a lot, Thanks alot. 

    They say working hard is good but i say working smart is best...

    Wednesday, November 7, 2012 5:16 AM