Skip to content

Instantly share code, notes, and snippets.

@DataKinds
Last active January 31, 2020 13:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DataKinds/ba8f1b6b5d0e7736e796 to your computer and use it in GitHub Desktop.
Save DataKinds/ba8f1b6b5d0e7736e796 to your computer and use it in GitHub Desktop.
circle modular multiplication chord renderer
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