Project1
标题:
计算两多边形是否香蕉的代码……
[打印本页]
作者:
yangff
时间:
2011-5-29 13:37
标题:
计算两多边形是否香蕉的代码……
module CV
module_function
class Point
def initialize(x,y)
@x=x.to_f
@y=y.to_f
end
def x
return @x
end
def y
return @y
end
def x=(v)
return (@x=v)
end
def y=(v)
return (@y=v)
end
def in_polygon(p)
p=p.points
nCross=0
for i in 0...p.size
p1=p[i]
p2=p[(i+1) % p.size]
next if p1.y == p2.y
next if y < [p1.y, p2.y].min
next if y >= [p1.y, p2.y].max
x1=(y - p1.y)*(p2.x - p1.x)/(p2.y - p1.y) + p1.x
#p (p2.y - p1.y)
nCross+=1 if x1>x
end
#p nCross
return (nCross % 2 == 1)
end
end
class Grap
end
class Polygon<Grap
def initialize(*args)
@points=args
end
def points
return @points
end
def &(v)
if v.is_a?(Grap)
if v.is_a?(Polygon)
for i in @points
if i.in_polygon(v)==true
return true
end
end
for i in v.points
if i.in_polygon(self)==true
return true
end
end
end
end
return false
end
end
class Vector
end
end
poy=CV::Polygon.new(CV::Point.new(0,0),CV::Point.new(1,2),CV::Point.new(5,5),CV::Point.new(5,15))
poy1=CV::Polygon.new(CV::Point.new(300,500),CV::Point.new(100,200),CV::Point.new(500,500),CV::Point.new(500,1500))
p poy&poy1
复制代码
效率一般……没做粗判定……
欢迎光临 Project1 (https://rpg.blue/)
Powered by Discuz! X3.1