Drawing ellipses

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
IdahoEv
Prole
Posts: 4
Joined: Wed Mar 23, 2011 6:00 pm

Drawing ellipses

Post by IdahoEv »

I hacked an addition to the current version of love that adds love.graphics.ellipse(). (I needed it to draw noncircular planetary orbits). I've sent a pull request to rude, but in case anyone else wants to draw ellipses in the meantime, you can get it here: https://bitbucket.org/IdahoEv/love

Usage:
love.graphics.ellipse(mode, x, y, a, b, phi, segments)

mode, x, y, segments: same as love.graphics.circle()
a: semimajor axis
b: semiminor axis
phi: angle between the major axis and the horizontal, defaults to 0.0

Example:

Code: Select all

--main.lua
draws = 0
function love.draw()
  love.graphics.setColor(0,0,255)
  love.graphics.ellipse('line',400,150,100,50)
  love.graphics.ellipse('fill',400,400,150,100, .01 * draws, 30)
  draws = draws + 1
end       
jericson
Prole
Posts: 1
Joined: Tue Apr 03, 2012 8:30 pm

Re: Drawing ellipses

Post by jericson »

Thanks for the code. I wait with eager anticipation for it to be incorporated into the official build. ;)

In the meantime, I've taken the liberty rewrite your function in C to be pure Lua so that I can use it without rebuilding the source:

Code: Select all

-- Ellipse in general parametric form 
-- (See http://en.wikipedia.org/wiki/Ellipse#General_parametric_form)
-- (Hat tip to IdahoEv: https://love2d.org/forums/viewtopic.php?f=4&t=2687)
--
-- The center of the ellipse is (x,y)
-- a and b are semi-major and semi-minor axes respectively
-- phi is the angle in radians between the x-axis and the major axis

function love.graphics.ellipse(mode, x, y, a, b, phi, points)
  phi = phi or 0
  points = points or 10
  if points <= 0 then points = 1 end

  local two_pi = math.pi*2
  local angle_shift = two_pi/points
  local theta = 0
  local sin_phi = math.sin(phi)
  local cos_phi = math.cos(phi)

  local coords = {}
  for i = 1, points do
    theta = theta + angle_shift
    coords[2*i-1] = x + a * math.cos(theta) * cos_phi 
                      - b * math.sin(theta) * sin_phi
    coords[2*i] = y + a * math.cos(theta) * sin_phi 
                    + b * math.sin(theta) * cos_phi
  end

  coords[2*points+1] = coords[1]
  coords[2*points+2] = coords[2]

  love.graphics.polygon(mode, coords)
end
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 3 guests