[从头学数学] 第265节 [计算几何] 多线段求交点(扫描线法)

剧情提要:
阿伟看到了一本比较有趣的书,是关于《计算几何》的,2008年由北清派出版。很好奇
它里面讲了些什么,就来看看啦。


正剧开始:
星历2016年09月19日 11:58:08, 银河系厄尔斯星球中华帝国江南行省。

[工程师阿伟]正在和[机器小伟]一起研究[计算几何]]。




本节的程序并没有最终完成,目前仅仅是没有语法错误,能运行而已,但运算逻辑上有一点问题,计算结果并不准确。


<span style="font-size:18px;">#
class Point():
    def __init__(self, point):
        self.point = point;

    def __lt__(self, other):
        if (type(other.point) != type([0, 0])):
            return self.point < other.point;
        else:
            p1 = self.point;
            p2 = other.point;

            if p1[1] < p2[1]:
                return True;
            elif p1[1] > p2[1]:
                return False;
            else:
                return p1[0] < p2[0];
            
    def __eq__(self, other):
        if other == None:
            return False;
        else:
            return self.point == other.point;

    def __str__(self):
        return str(self.point);

    def __repr__(self):
        return 'Point('+str(self)+')';

    def __hash__(self):
        return hash(str(self));

#</span>


<span style="font-size:18px;">#
#线段类
class SegLine():
    def __init__(self, pStart=[1e6,1e6], pEnd=[1e6, 1e6]):
        #按照y值由小到大,x值由小到大的顺序排列
        if pStart[1] > pEnd[1]:
            pStart, pEnd = pEnd, pStart;
        elif pStart[1] == pEnd[1]:
            if pStart[0] > pEnd[0]:
                pStart, pEnd = pEnd, pStart;
                
        self.pStart = pStart;
        self.pEnd = pEnd;

    #<号的判定是根据起点和终点的位置
    #y值权重优先
    def __lt__(self, other):
        if type(self) == type(None) or type(other) == type(None):
            return False;
        
        if self.pStart == other.pStart:
            if self.pEnd[1] < other.pEnd[1]:
                return True;
            elif self.pEnd[1] == other.pEnd[1]:
                if self.pEnd[0] < other.pEnd[0]:
                    return True;
            else:
                return False;
        else:
            if self.pStart[1] < other.pStart[1]:
                return True;
            elif self.pStart[1] == other.pStart[1]:
                if self.pStart[0] < other.pStart[0]:
                    return True;
            else:
                return False;        

    def __eq__(self, other):
        if type(self) == type(None) or type(other) == type(None):
            return False;
        
        if self.pStart == other.pStart and self.pEnd == other.pEnd:
            return True;

        return False;

    def __gt__(self, other):
        if self == other or self < other:
            return False;

        return True;

    def __ge__(self, other):
        return (not self < other) or (self == other);

    def __str__(self):
        x1, y1 = self.pStart[0],  self.pStart[1];
        x2, y2 = self.pEnd[0], self.pEnd[1];
        
        return '[['+str(x1)+', '+str(y1)+'], ['+str(x2)+', '+str(y2)+']]';

    def __hash__(self):
        return hash(str(self));

    def __repr__(self):
        return 'SegLine('+str(self)[1:-1]+')';

#</span>


<s
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值