How to think like a Computer Scientist: 课后习题第十五章1-5

#-------------------------------------------------------------------------------
# Name:        module2
# Purpose:
#
# Author:      penglaixy
#
# Created:     31/08/2013
# Copyright:   (c) penglaixy 2013
# Licence:     <your licence>
#-------------------------------------------------------------------------------
import sys

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def distance_between_points(self, target):
        return ((self.x-target.x)**2 + (self.y-target.y)**2)**0.5

    def __str__(self):
        return ("({0} {1})".format(self.x,self.y))

    def reflect_x(self):
        return  ("({0} {1})".format(self.x,-self.y))

    def slope_from_origin(self):
        if self.x != 0:
            return float(self.y)/self.x
        else:
            return None

    def get_line_to(self,target):
        if self.y == target.y:
            return 0, self.y
        elif self.x == target.x:
            return None, self.x
        else:
            m = (float(target.y - self.y))/(target.x-self.x)
            c = self.y - m*self.x
            return m,c

def midPoint(p1,p2):
    mx = (p1.x + p2.x)/2
    my = (p1.y + p2.y)/2
    return (mx,my)

def cross_point_of_two_lines(k1, b1, k2, b2):
    cx = (b2 - b1)/(k1 - k2)
    cy = k1*cx + b1
    return Point(cx,cy)

def caculate_circle_point(p1,p2,p3,p4):
    # caculate two lines
    k1,b1 = p1.get_line_to(p2)
    k2,b2 = p3.get_line_to(p4)

    #print k1,b1,k2,b2

    if k1 == k2:
        print "lines are pararall, points can not in one circle!"
        return None

    if k1 == None and k2 == 0:
        return b1,b2

    if k1 == 0 and k2 == None:
        return b2,b1


    #caculate  new two lines cross the two lines forewards
    k3 = -1.0/k1
    k4 = -1.0/k2

    m1,b1 = midPoint(p1,p2)
    m2,b2 = midPoint(p3,p4)

    b3 = b1 - m1*k3
    b4 = b2 - m2*k4

    #print k3,b3,k4,b4

    p = cross_point_of_two_lines(k3,b3,k4,b4)

    d1 = p.distance_between_points(p1)
    print "Point:{0} and point:{1} distance is : {2}".format(p,p1,d1)

    d2 = p.distance_between_points(p3)
    print "Point:{0} and point:{1} distance is : {2}".format(p,p3,d2)

    if abs(d1 - d2) > 0.01:
        print "These four point can't format a circle!"
        return None
    else:
        print "These four point can find format a circle, and the circle center\
 is {0}!".format(p)
        return p

def test(did_pass):
    '''print the result of a test '''
    linenum=sys._getframe(1).f_lineno
    if did_pass:
        msg = "Test at line{0} ok".format(linenum)
    else:
        msg = "Test at line{0} failed".format(linenum)
    print msg


def main():
    p1 = Point(1,5)
    p2 = Point(1,-5)
    p3 = Point(-6,3)
    p4 = Point(6, 3)

    print "Point:{0} and point:{1} distance is : {2:.4f}".format(p1,p2,
     p1.distance_between_points(p2))

    print "Point:{0} reflect_x is {1}".format(p1,p1.reflect_x())

    print "Point:{0} slope from (0,0) is {1:.1f}".format(p1,p1.slope_from_origin())

    k,b = Point(4,11).get_line_to(Point(6,15))

    if k == None:
        print "Line is : x = {0}".format(b)

    if k == 0:
        print "Line is : y = {0}".format(b)

    print "Line is : y = {0}x + {1}".format(k,b)




    print "Circle center is {0}.".format(caculate_circle_point(p1,p2,p3,p4))



if __name__ == '__main__':
    main()
>>> 
*** Remote Interpreter Reinitialized  ***
>>> 
Point:(1 5) and point:(1 -5) distance is : 10.0000
Point:(1 5) reflect_x is (1 -5)
Point:(1 5) slope from (0,0) is 5.0
Line is : y = 2.0x + 3.0
Circle center is (1, 3).


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值