Created
May 1, 2012 01:14
-
-
Save awostenberg/2564133 to your computer and use it in GitHub Desktop.
calculate points on the circumference of a circle
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
| # -*- coding: utf-8 -*- | |
| # A simple class to calculate approximately any point on a circle defined by the radius | |
| class Circle | |
| def initialize(radius) # Might be actually the diameter; just whipping up some quick code | |
| @radius = radius | |
| end | |
| def [](theta) | |
| # π | |
| # You must multiply --- to convert theta from degrees to radians | |
| # 180 | |
| t = theta * Math::PI / 180.0 | |
| x = Math.sin(t) * @radius | |
| y = Math.cos(t) * @radius | |
| [x, y] | |
| end | |
| end | |
| # Create a circle with a radius of 50 units | |
| c = Circle.new(50) | |
| # Create a list of evenly spread angles to query. This should get the points on the circle at the angles | |
| # 90, 180, 270, and 360 | |
| max = 4 | |
| angles = (1..max).collect {|d| 360 * (d.to_f / max)} | |
| # Now to print our info to the user | |
| puts "For a circle with a radius of 50:" | |
| angles.each do |a| | |
| pt = c[a] | |
| puts " • The point at #{a}° on the circle is approximately (#{pt[0]}, #{pt[1]})" | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment