如何“旋转”椭圆?

时间:2018-10-31 15:54:37

标签: lua ellipse love2d

使用此:

local W, H = 100, 50

function love.draw()
  love.graphics.translate(love.graphics.getWidth()/2,love.graphics.getHeight()/2)
  for i = 1, 360 do
    local I = math.rad(i)
    local x,y = math.cos(I)*W, math.sin(I)*H
    love.graphics.line(0, 0, x, y)
  end
end

我可以将直线与椭圆的中心(长度为W,高度为H)和边缘相连。如何使用参数R“旋转”椭圆的中心?我知道您可以使用love.graphics.ellipselove.graphics.rotate来做到这一点,但是有什么办法可以获取旋转椭圆上的点的坐标?

1 个答案:

答案 0 :(得分:3)

这是一个三角学问题,这是基本2D旋转的工作方式。想象一个位于(x,y)的点。如果您想围绕原点旋转该点(在您的情况下为0,0)一个角度θ,则新点的坐标将通过以下变换位于(x1,y1)处

  

x1 = x cosθ− y sinθ
  y1 = y cosθ+ x sinθ

在您的示例中,旋转后我添加了一个新的椭圆形

function love.draw()
    love.graphics.translate(love.graphics.getWidth()/2,love.graphics.getHeight()/2)
    for i = 1, 360, 5 do
        local I = math.rad(i)
        local x,y = math.cos(I)*W, math.sin(I)*H
        love.graphics.setColor(0xff, 0, 0) -- red
        love.graphics.line(0, 0, x, y)
    end

  -- rotate by angle r = 90 degree
    local r = math.rad(90)
    for i = 1, 360, 5 do
        local I  = math.rad(i)
        -- original coordinates
        local x  = math.cos(I) * W 
        local y  = math.sin(I) * H
        -- transform coordinates
        local x1 = x * math.cos(r) - y * math.sin(r) 
        local y1 = y * math.cos(r) + x * math.sin(r) 
        love.graphics.setColor(0, 0, 0xff) -- blue
        love.graphics.line(0, 0, x1, y1)
    end
end

enter image description here

相关问题