-
-
Save NewAlexandria/81bfa311594a473ae29c to your computer and use it in GitHub Desktop.
circle modular multiplication chord renderer
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
# with reference to rendered output but the OP | |
# https://www.reddit.com/r/mathpics/comments/3yzu77/heres_an_animated_version_of_that_circular/ | |
# and a video guide | |
# https://www.youtube.com/watch?v=qhbuKbxJsk8 | |
require "rmagick" | |
RESOLUTION = 800 | |
def localToGlobalCoords(n) | |
#local go from -1 to 1 | |
#global go from 0 to RESOLUTION | |
#x always equals y, the image is square | |
return ((n + 1)*(RESOLUTION/2)).floor | |
end | |
def ratioToX(n) | |
return Math.cos(n*2*Math::PI) | |
end | |
def ratioToY(n) | |
return Math.sin(n*2*Math::PI) | |
end | |
def ratioToCoords(n) | |
return {x: ratioToX(n), y: ratioToY(n)} | |
end | |
def makeCircle(factor, modulo) | |
#points = [] | |
#modulo.times do |index| | |
# points.push({number: index, x: ratioToX(index.to_f/modulo), y: ratioToY(index.to_f/modulo)}) | |
#end | |
chords = [] | |
modulo.times do |index| | |
chords.push({startPoint: ratioToCoords(index.to_f/modulo), endPoint: ratioToCoords(((factor*index.to_f)%modulo)/modulo)}) | |
end | |
#return {points: points, chords: chords} | |
return chords | |
end | |
def main | |
currentModulo = 500 | |
(1..20000).each do |currentFactorInt| | |
currentFactor = Rational(currentFactorInt, 100) | |
circle = makeCircle(currentFactor, currentModulo) | |
finalImage = Magick::Image.new(RESOLUTION, RESOLUTION) { self.background_color = "black" } | |
draw = Magick::Draw.new | |
draw.fill("white") | |
draw.stroke("black") | |
draw.circle(RESOLUTION/2, RESOLUTION/2, 0, RESOLUTION/2) | |
currentHue = (currentFactorInt * 2) % 360 | |
circle.each do |currentChord| | |
draw.stroke("hsb(#{currentHue.floor}, 255, 128)") | |
currentHue += 0.2 | |
currentHue = currentHue % 360 | |
draw.line(localToGlobalCoords(currentChord[:startPoint][:x]), localToGlobalCoords(currentChord[:startPoint][:y]), | |
localToGlobalCoords(currentChord[:endPoint][:x]), localToGlobalCoords(currentChord[:endPoint][:y])) | |
end | |
draw.annotate(finalImage, 0, 0, 10, 10, "0 ≤ N ≤ #{currentModulo}") { | |
self.font = "Droid-Sans-Mono" | |
self.fill = "white" | |
self.stroke = "transparent" | |
self.pointsize = 20 | |
self.gravity = Magick::NorthEastGravity | |
} | |
draw.annotate(finalImage, 0, 0, 10, 10, "(#{currentFactor} * N) mod #{currentModulo}") { | |
self.font = "Droid-Sans-Mono" | |
self.fill = "white" | |
self.stroke = "transparent" | |
self.pointsize = 20 | |
self.gravity = Magick::SouthEastGravity | |
} | |
draw.draw(finalImage) | |
finalImage.write("frames/%06d.png" % currentFactorInt) | |
puts "wrote frame #{currentFactorInt}/#{20000}, factor #{currentFactor.to_s}" | |
end | |
end | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment