【问题标题】:How to "rotate" an ellipse?如何“旋转”椭圆?
【发布时间】:2018-10-31 15:54:36
【问题描述】:

使用这个:

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 来做到这一点,但有什么办法可以得到旋转椭圆上点的坐标?

【问题讨论】:

    标签: lua ellipse love2d


    【解决方案1】:

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

    x1 = xcosθ - ysinθ
    y1 = ycosθ + xsinθ

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

    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
    

    【讨论】:

      猜你喜欢
      • 2021-10-25
      • 1970-01-01
      • 1970-01-01
      • 2017-08-31
      • 1970-01-01
      • 2015-10-03
      • 1970-01-01
      • 2021-12-24
      • 1970-01-01
      相关资源
      最近更新 更多