Created
August 1, 2016 19:09
-
-
Save ggcrunchy/c46819798c15ca5c7e07b15c4faf0015 to your computer and use it in GitHub Desktop.
A little complex number test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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