Last active
September 18, 2016 02:15
-
-
Save wconrad/767808d9d5340f7df67b80bf19612e79 to your computer and use it in GitHub Desktop.
Ruby class to encapsulate an angle
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
module CpuSim::Cpu | |
class Angle | |
def self.zero | |
new(0) | |
end | |
def self.radians(v) | |
new(v) | |
end | |
def self.degrees(v) | |
radians(v / 180 * Math::PI) | |
end | |
def self.revolutions(v) | |
radians(v * 2 * Math::PI) | |
end | |
private_class_method :new | |
attr_reader :radians | |
def initialize(radians) | |
@radians = radians | |
end | |
def revolutions | |
@radians / (2 * Math::PI) | |
end | |
def degrees | |
revolutions * 360 | |
end | |
def normalize | |
self.class.radians(@radians % (2 * Math::PI)) | |
end | |
def +(other) | |
self.class.radians(@radians + other.radians) | |
end | |
end | |
end |
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
module CpuSim::Cpu | |
RSpec.describe Angle do | |
it "should make a zero angle" do | |
angle = Angle.zero | |
expect(angle.radians).to eq 0 | |
end | |
it "should be created from revolutions" do | |
angle = Angle.revolutions(0.5) | |
expect(angle.radians).to be_within(0.001).of(Math::PI) | |
end | |
it "should be created from degrees" do | |
angle = Angle.degrees(180) | |
expect(angle.radians).to be_within(0.001).of(Math::PI) | |
end | |
it "should be zero radians" do | |
angle = Angle.radians(0) | |
expect(angle.radians).to eq 0 | |
end | |
it "should be PI radians" do | |
angle = Angle.radians(Math::PI) | |
expect(angle.radians).to eq Math::PI | |
end | |
it "should be 2 PI radians" do | |
angle = Angle.radians(2 * Math::PI) | |
expect(angle.radians).to eq 2 * Math::PI | |
end | |
it "should normalize 0" do | |
angle = Angle.zero | |
angle = angle.normalize | |
expect(angle.radians).to eq 0 | |
end | |
it "should normalize -2 * PI radians" do | |
angle = Angle.radians(-2 * Math::PI) | |
angle = angle.normalize | |
expect(angle.radians).to eq 0 | |
end | |
it "should normalize -PI radians" do | |
angle = Angle.radians(-Math::PI) | |
angle = angle.normalize | |
expect(angle.radians).to eq Math::PI | |
end | |
it "should normalize PI radians" do | |
angle = Angle.radians(Math::PI) | |
angle = angle.normalize | |
expect(angle.radians).to eq Math::PI | |
end | |
it "should normalize 2 PI radians" do | |
angle = Angle.radians(2 * Math::PI) | |
angle = angle.normalize | |
expect(angle.radians).to eq 0 | |
end | |
it "should normalize 3 PI radians" do | |
angle = Angle.radians(3 * Math::PI) | |
angle = angle.normalize | |
expect(angle.radians).to eq Math::PI | |
end | |
it "should add two angles" do | |
angle1 = Angle.radians(Math::PI) | |
angle2 = Angle.radians(2 * Math::PI) | |
angle3 = angle1 + angle2 | |
expect(angle3.radians).to eq(3 * Math::PI) | |
end | |
it "should know its number of revolutions" do | |
angle = Angle.radians(3 * Math::PI) | |
expect(angle.revolutions).to be_within(0.001).of(1.5) | |
end | |
it "should know its number of degrees" do | |
angle = Angle.radians(3 * Math::PI) | |
expect(angle.degrees).to be_within(0.001).of(540) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment