Skip to content

Instantly share code, notes, and snippets.

@ggcrunchy
Created August 1, 2016 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggcrunchy/c46819798c15ca5c7e07b15c4faf0015 to your computer and use it in GitHub Desktop.
Save ggcrunchy/c46819798c15ca5c7e07b15c4faf0015 to your computer and use it in GitHub Desktop.
A little complex number test
local CX, CY = display.contentCenterX, display.contentCenterY
local N = 15
local R = 50
local cosa, sina = R, 0 -- can be R, 0 if you have a fixed radius; otherwise, you will scale these
-- intermediate steps
local cosda, sinda = math.cos(2 * math.pi / N), math.sin(2 * math.pi / N)
-- https://math.stackexchange.com/questions/465070/multiplying-two-complex-numbers-using-only-three-multiplications-of-real-numbers
-- a = cos(A), b = sin(A), c = cos(dA), d = sin(dA)
-- P1 = ac, P2 = bd, P3 = (a + b)(c + d)
local c_plus_d = cosda + sinda
for i = 1, N do
-- do something with (cosa, sina)
local cc = display.newCircle(CX + cosa, CY + sina, 5)
cc:setFillColor(0, 0, 1)
-- Rotate by angle delta.
local P1, P2, P3 = cosa * cosda, sina * sinda, (cosa + sina) * c_plus_d
cosa, sina = P1 - P2, P3 - P2 - P1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment