Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Shape rotation — Gideros Forum

Shape rotation

javabienjavabien Member
edited December 2013 in General questions
Hello,

I read all forum article regards Shape and rotation without find a real solution. Then ....

I create my gideros class derived from Shape class. In fact, I want to create an real arc draw (acr + the two radius). My class is working but now I need to rotate my new draw. When I use myObject:setRotation(60) the image not rotate to the center point of my arc. Could someone help me to rotate my draw in the correct manner ?

Please find my code here :
------------------------------------------------------------------
Arc = gideros.class(Shape)
function Arc:init(t)
local x = t.x or t.y or 0
local y = t.y or t.x or 0
local fillStyle = t.fillStyle or { Shape.SOLID, 0x000000, 0.5 }
local lineStyle = t.lineStyle or { 2, 0xffffff, 0.5 }
local xradius = t.xradius or t.radius or 100
local yradius = t.yradius or t.radius or 100
local sides = t.sides or (xradius + yradius)/2
local arcAngle = t.arcAngle or 0
self:clear()
self:setFillStyle(unpack(fillStyle))
self:setLineStyle(unpack(lineStyle))

local angleStep = arcAngle / sides



self:beginPath()

self:moveTo(x, y)


for i = 1, arcAngle do

self:lineTo(x+math.cos(math.rad(i))*xradius, y+math.sin(math.rad(i))*yradius)
end

self:lineTo(x, y)
self:endPath()


if t.alpha then
self:setAlpha(t.alpha)
end
if t.parent then
t.parent:addChild(self)
end

return self
end
---------------------------------------------------------------------

Comments

  • Hello @javabien
    I suppose you want to rotate the shape by its center.
    Well the shape is rotated by it's 0,0 coordinate.

    For example, if you draw rectangle like this:
    local shape = Shape.new()
    shape:beginPath()
    shape:moveTo(0,0)
    shape:lineTo(0, 100)
    shape:lineTo(100, 100)
    shape:lineTo(100, 0)
    shape:closePath()
    shape:endPath()
    then it will rotate by its corner, because 0,0 coordinate is on the corner

    But if you draw it like this:
    local shape = Shape.new()
    shape:beginPath()
    shape:moveTo(-50, -50)
    shape:lineTo(-50, 50)
    shape:lineTo(50, 50)
    shape:lineTo(50, -50)
    shape:closePath()
    shape:endPath()
    the 0,0 coordinate is at the center of rectangle and it will rotate around this center.

    Same way you should draw your arcs, by leaving 0,0 coordinate as the center of the circle

    Also I think some drawing additions on Shape were here:
    https://github.com/ar2rsawseen/GiderosCodingEasy/
    You can take a peak ;)
  • john26john26 Maintainer
    It's better not to define and position your shape in one command. Instead create it on (0,0) and then use a separate setPosition command.

    E.g. consider the built in graphics:

    t=TextField.new(myfont,"my text")

    t is now created as a textfield at (0,0). You can now position it

    t:setPosition(100,200)

    The creation command should only create the object. This is a good convention to follow.
  • Hello,

    thx ar2rsawseen and john26 . Build the arc at (0,0) and move it with setPosition solve my issue.

    Now I can move the arc from the center of it !

    Thanks for your help.
Sign In or Register to comment.