Created
December 13, 2008 13:04
-
-
Save cocontusfine/35465 to your computer and use it in GitHub Desktop.
undefined
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
#! /usr/bin/ruby | |
# -*- coding: utf-8 -*- | |
class Position | |
def initialize(x,y) | |
@x = x | |
@y = y | |
end | |
def x | |
@x | |
end | |
def y | |
@y | |
end | |
def to_s | |
"(#{@x},#{@y})" | |
end | |
def tan(pos) | |
dis_x = @x - pos.x | |
dis_y = @y - pos.y | |
dis_y/dis_x | |
end | |
end | |
class Triangle | |
def initialize(pos1,pos2,pos3) | |
@pos1 = pos1 | |
@pos2 = pos2 | |
@pos3 = pos3 | |
end | |
def pos1 | |
@pos1 | |
end | |
def pos2 | |
@pos2 | |
end | |
def pos3 | |
@pos3 | |
end | |
def area | |
vect1_x = @pos2.x - @pos1.x | |
vect1_y = @pos2.y - @pos1.y | |
vect2_x = @pos3.x - @pos1.x | |
vect2_y = @pos3.y - @pos1.y | |
i = vect1_x * vect2_y - vect1_y * vect2_x | |
i.abs | |
i/2 | |
end | |
def include?(pos) | |
@pos2.tan(@pos1) < pos.tan(@pos1) and pos.tan(@pos1) < @pos3.tan(@pos1) and @pos3.tan(@pos2) < pos.tan(@pos2) and pos.tan(@pos2) < @pos1.tan(@pos2) | |
end | |
end | |
# 座標を作って管理できるようにする | |
pos1 = Position.new(0.0,0.0) | |
pos2 = Position.new(4.0,0.0) | |
pos3 = Position.new(2.0,2.0) | |
p pos1 | |
p pos2 | |
p pos3 | |
# 二つの座標の傾きをtanで出す | |
tan1_2 = pos2.tan(pos1) | |
tan1_3 = pos3.tan(pos1) | |
tan2_3 = pos3.tan(pos2) | |
tan2_1 = pos1.tan(pos2) | |
p tan1_2 | |
p tan1_3 | |
p tan2_3 | |
p tan2_1 | |
# 三角形を作って管理できるようにする | |
triangle = Triangle.new(Position.new(0,0),Position.new(4.0,0.0),Position.new(2.0,2.0)) | |
# 面積を出す | |
puts "面積#{triangle.area}" | |
# 座標を三角形の中に含むかどうかをtan二つを使い判定する | |
puts triangle.include?(Position.new(1.0,0.5)) | |
puts triangle.include?(Position.new(4.0,2.0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment