《python语言程序设计》2018版第8章19题几何Rectangle2D类(下)-头疼的几何和数学

希望这个下集里能有完整的代码

一、containsPoint实现

mac系统里似乎不接受我们的驼峰命名,所以这个函数应该是contains_point

代码段如下,出现的问题0.05后Statement expected, found Py:DEDENT,完又遇到这个哥们了

python 复制代码
    def get_x(self):
        return self.__x

    def get_y(self):
        return self.__y

    # 实现另一个矩形的x,y坐标与当前矩形x,y对比
    def contains_points(self, other_x, other_y):
        a_t_valid = other_x - self.__x
        b_t_valid = other_y - self.__y
        a = (pow(a_t_valid, 2) + pow(b_t_valid, 2)) * 0.05

二、我要解决这个哥哥

先从网上找一下Statement expected, found Py:DEDENT

语句预期,发现Py:DEDEN Statement expected, found Py:DEDENT

AI解释的.我看不懂.估计是阅读恐惧问题.

我想想从里面找找我能看到的地方吧

TAB还是空格呢??

记得有人说.用空格的程序员很宝贵,要比用TAB的宝贵.看来这句话今天可以试试了

画圈的地方是我用空格键一个个按出来的.而不是用TAB按出来的.

小小总结

看来多用空格按钮

三、如何在矩形中对比

圆形我们以半径的长度为x,y的对比.矩形怎么办呢??

我之前已经有个办法了.那么我要去找一下第4章的兄弟帮忙

我调用了24.2第11次做的4.31题的代码

python 复制代码
  def count_point_judge(r1x, r1y, r2x, r2y, r3x, r3y):
        judgeLine1 = ((r1x - r2x) * (r3y - r2y)) - ((r3x - r2x) * (r1y - r2y))
        if judgeLine1 < 0:
            print("P2 x{} y{} is on the left side of the line from P0 to P1)".format(r3x, r3y))
        elif judgeLine1 > 0:
            print("P2 x{} y{} is on the right side of the line from P0 to P1)".format(r3x, r3y))
        elif judgeLine1 == 0:
            print("P2 x{} y{} is on the same line from P0 to P1)".format(r3x, r3y))

我似乎看到了希望.但是我发现它是用已知的2个点组成的线段对比另一个已知的点.

四、兜兜转转的我要拆分矩形的四个点

本来就是一个偷懒的完成矩形就可以了.但是.但是看来玩要好好的拆分了.

如何拆分矩形的四个点呢.

我们应该利用当前的x,y坐标和长宽来计算四个点的坐标.

python 复制代码
# 求得最右边的代码
(x_1 - width_r / 2, height_r / 2 + y_1)

好我们建立一个计算的函数吧

五、计算矩形四个x,y点的con_po登场

python 复制代码
   def con_po(self):
        rx1, ry1 = self.__x - self.__width / 2, self.__height / 2 + self.__y
        rx2, ry2 = abs(rx1) + self.__width, ry1
        rx3, ry3 = rx2, ry2 - self.__height
        rx4, ry4 =abs(rx1)-self.__height, ry3
        turtle.penup()
        turtle.goto(rx1,ry1)
        turtle.pendown()
        turtle.goto(rx2,ry2)
        turtle.goto(rx3,ry3)
        turtle.goto(rx4,ry4)
        turtle.hideturtle()
        turtle.done()
我们来小小的测试一下这个函数
python 复制代码
def main():
    x1, y1, width1, height1 = eval(input("Enter x1,y1,width1,height1: "))
    r1 = Rectangle2D(x1, y1, width1, height1)
    r1.con_po()

main()
结果

别慌,我们把点标出来.

python 复制代码
# 增加一段打印代码来看看这4对坐标的数值
print(f"x1: {rx1}, y1: {ry1}")
print(f"x2: {rx2}, y2: {ry2}")
print(f"x3: {rx3}, y3: {ry3}")
print(f"x4: {rx4}, y4: {ry4}")

以下数据为例.按我的代码计算结果截图
x1, y1, width1, height1 = 9,1.3,10,35.3

出在哪里呢???

我怀疑是x4的计算上.我们合计一下.

python 复制代码
        # 我是不是不应该进行这样的减法
        rx4, ry4 =abs(rx1)-self.__height, ry3
修改完成
python 复制代码
 def con_po(self):
        rx1, ry1 = self.__x - self.__width / 2, self.__height / 2 + self.__y
        rx2, ry2 = abs(rx1) + self.__width, ry1
        rx3, ry3 = rx2, ry2 - self.__height
        rx4, ry4 = rx1, ry3
        print(f"x1: {rx1}, y1: {ry1}")
        print(f"x2: {rx2}, y2: {ry2}")
        print(f"x3: {rx3}, y3: {ry3}")
        print(f"x4: {rx4}, y4: {ry4}")
        turtle.penup()
        turtle.goto(rx1, ry1)
        turtle.dot(3, "blue")
        turtle.pendown()
        turtle.goto(rx2, ry2)
        turtle.dot(3, "blue")
        turtle.goto(rx3, ry3)
        turtle.dot(3, "blue")
        turtle.goto(rx4, ry4)
        turtle.dot(3, "blue")
        turtle.goto(rx1, ry1)
        turtle.hideturtle()

现在我们获得了坐标接下来我们应该如何把con_po融入到contains_points

variable in function should be lowercase 函数变量应该小写

六、结合后的contains_points

python 复制代码
    def contains_points(self,other_x,other_y):
        rx1, ry1 = self.__x - self.__width / 2, self.__height / 2 + self.__y
        rx2, ry2 = abs(rx1) + self.__width, ry1
        rx3, ry3 = rx2, ry2 - self.__height
        rx4, ry4 = rx1, ry3
        judge_line1 = ((rx1 - rx2) * (other_y - ry2)) - ((other_x - rx2) * (ry1 - ry2))
        judge_line2 = ((rx2 - rx3) * (other_y - ry3)) - ((other_x - rx3) * (ry2 - ry3))
        judge_line3 = ((rx3 - rx4) * (other_y - ry4)) - ((other_x - rx4) * (ry3 - ry4))
        judge_line4 = ((rx4 - rx1) * (other_y - ry1)) - ((other_x - rx1) * (ry4 - ry1))

        if judge_line1 > 0 and judge_line2 >0 and judge_line3 >0 and judge_line4 >0:
            print(f" r2 x: {other_x} y: {other_y} is in r1")
        else:
            print(f" r2 x: {other_x} y: {other_y} is not in r1")

七、本题最后的代码

这次偷懒了.就不再分析下面的问题了.但是代码写全了

python 复制代码
class Rectangle2D:
    # 初始化当前矩形的信息
    def __init__(self, x, y, width, height):
        self.__x = x
        self.__y = y
        self.__width = width
        self.__height = height

    # 获得当前矩形的面积
    def get_area(self):
        return self.__width * self.__height

    # 获得当前矩形的周长
    def get_perimeter(self):
        return (self.__width + self.__height) * 2

    # 装一下,打印该矩形的x,y和长宽
    def print_text(self, name):
        print(f"Enter x{name}, y{name}, width{name}, height{name}: {self.__x}, {self.__y}, {self.__width},"
              f" {self.__height}")

    # 绘制当前矩形
    def draw_rec1(self):
        turtle.penup()
        turtle.goto(self.__x, self.__y)
        turtle.dot(6, "blue")
        turtle.goto(self.__x - self.__width / 2, self.__height / 2 + self.__y)
        turtle.pendown()
        turtle.forward(self.__width)
        turtle.right(90)
        turtle.forward(self.__height)
        turtle.right(90)
        turtle.forward(self.__width)
        turtle.right(90)
        turtle.forward(self.__height)
        turtle.hideturtle()
        turtle.done()

    # 绘制当前矩形和另一个矩形
    def draw_rec2(self, other_rec):
        x_1 = other_rec.__x
        y_1 = other_rec.__y
        width_r = other_rec.__width
        height_r = other_rec.__height
        turtle.penup()
        turtle.goto(self.__x, self.__y)
        turtle.dot(6, "black")
        turtle.goto(self.__x - self.__width / 2, self.__height / 2 + self.__y)
        turtle.pendown()
        turtle.forward(self.__width)
        turtle.right(90)
        turtle.forward(self.__height)
        turtle.right(90)
        turtle.forward(self.__width)
        turtle.right(90)
        turtle.forward(self.__height)
        turtle.left(270)

        turtle.penup()
        turtle.goto(x_1, y_1)
        turtle.dot(6, "blue")
        turtle.goto(x_1 - width_r / 2, height_r / 2 + y_1)
        turtle.pendown()
        turtle.forward(width_r)
        turtle.right(90)
        turtle.forward(height_r)
        turtle.right(90)
        turtle.forward(width_r)
        turtle.right(90)
        turtle.forward(height_r)
        turtle.hideturtle()
        turtle.done()

    def get_x(self):
        return self.__x

    def get_y(self):
        return self.__y

    # 为了计算4对坐标设计
    def con_po(self):
        rx1, ry1 = self.__x - self.__width / 2, self.__height / 2 + self.__y
        rx2, ry2 = abs(rx1) + self.__width, ry1
        rx3, ry3 = rx2, ry2 - self.__height
        rx4, ry4 = rx1, ry3
        print(f"x1: {rx1}, y1: {ry1}")
        print(f"x2: {rx2}, y2: {ry2}")
        print(f"x3: {rx3}, y3: {ry3}")
        print(f"x4: {rx4}, y4: {ry4}")
        turtle.penup()
        turtle.goto(rx1, ry1)
        turtle.dot(3, "blue")
        turtle.pendown()
        turtle.goto(rx2, ry2)
        turtle.dot(3, "blue")
        turtle.goto(rx3, ry3)
        turtle.dot(3, "blue")
        turtle.goto(rx4, ry4)
        turtle.dot(3, "blue")
        turtle.goto(rx1, ry1)
        turtle.hideturtle()
        turtle.done()

    # 实现另一个矩形的x,y坐标与当前矩形x,y对比
    def contains_points(self, other_x, other_y):
        rx1, ry1 = self.__x - self.__width / 2, self.__height / 2 + self.__y
        rx2, ry2 = abs(rx1) + self.__width, ry1
        rx3, ry3 = rx2, ry2 - self.__height
        rx4, ry4 = rx1, ry3
        judge_line1 = ((rx1 - rx2) * (other_y - ry2)) - ((other_x - rx2) * (ry1 - ry2))
        judge_line2 = ((rx2 - rx3) * (other_y - ry3)) - ((other_x - rx3) * (ry2 - ry3))
        judge_line3 = ((rx3 - rx4) * (other_y - ry4)) - ((other_x - rx4) * (ry3 - ry4))
        judge_line4 = ((rx4 - rx1) * (other_y - ry1)) - ((other_x - rx1) * (ry4 - ry1))

        if judge_line1 > 0 and judge_line2 > 0 and judge_line3 > 0 and judge_line4 > 0:
            print(f"r1 contains the center of r2? True")
        else:
            print(f"r1 contains the center of r2? False")


# 包括面积、周长计算
def main():
    # x1, y1, width1, height1 = eval(input("Enter x1,y1,width1,height1: "))
    # x2, y2, width2, height2 = eval(input("Enter x2,y2,width2,height2: "))
    x1, y1, width1, height1 = 9, 1.3, 10, 35.3
    x2, y2, width2, height2 = 1.3, 4.3, 4, 5.3

    r1 = Rectangle2D(x1, y1, width1, height1)
    r1.print_text(1)
    area_r1 = r1.get_area()
    print(f"Area for r1 is {area_r1}")
    perimeter_r1 = r1.get_perimeter()
    print(f"Perimeter for r1 is {perimeter_r1}")

    r2 = Rectangle2D(x2, y2, width2, height2)
    r2.print_text(2)
    area_r2 = r2.get_area()
    print(f"Area for r2 is {area_r2}")
    perimeter_r2 = r2.get_perimeter()
    print(f"Perimeter for r2 is {perimeter_r2}")

    r1.contains_points(r2.get_x(), r2.get_y())

main()

明天继续,加油通知们.

相关推荐
记录成长java21 分钟前
ServletContext,Cookie,HttpSession的使用
java·开发语言·servlet
前端青山21 分钟前
Node.js-增强 API 安全性和性能优化
开发语言·前端·javascript·性能优化·前端框架·node.js
睡觉谁叫~~~25 分钟前
一文解秘Rust如何与Java互操作
java·开发语言·后端·rust
音徽编程25 分钟前
Rust异步运行时框架tokio保姆级教程
开发语言·网络·rust
观音山保我别报错26 分钟前
C语言扫雷小游戏
c语言·开发语言·算法
小屁孩大帅-杨一凡1 小时前
java后端请求想接收多个对象入参的数据
java·开发语言
m0_656974741 小时前
C#中的集合类及其使用
开发语言·c#
java1234_小锋1 小时前
使用 RabbitMQ 有什么好处?
java·开发语言
wjs20242 小时前
R 数据框
开发语言
肘击鸣的百k路2 小时前
Java 代理模式详解
java·开发语言·代理模式