当前位置: 首页 > article >正文

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

在这里插入图片描述

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

一、containsPoint实现

      • 先从网上找一下Statement expected, found Py:DEDENT
      • TAB还是空格呢??
      • 小小总结
      • 如何拆分矩形的四个点呢.
        • 我们来小小的测试一下这个函数
        • 结果
        • 出在哪里呢???
        • 修改完成
        • variable in function should be lowercase 函数变量应该小写

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

在这里插入图片描述

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

    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
      • TAB还是空格呢??
      • 小小总结
      • 如何拆分矩形的四个点呢.
        • 我们来小小的测试一下这个函数
        • 结果
        • 出在哪里呢???
        • 修改完成
        • variable in function should be lowercase 函数变量应该小写

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

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

在这里插入图片描述
AI解释的.我看不懂.估计是阅读恐惧问题.
我想想从里面找找我能看到的地方吧

TAB还是空格呢??

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

在这里插入图片描述

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

小小总结

看来多用空格按钮

三、如何在矩形中对比

      • 先从网上找一下Statement expected, found Py:DEDENT
      • TAB还是空格呢??
      • 小小总结
      • 如何拆分矩形的四个点呢.
        • 我们来小小的测试一下这个函数
        • 结果
        • 出在哪里呢???
        • 修改完成
        • variable in function should be lowercase 函数变量应该小写

圆形我们以半径的长度为x,y的对比.矩形怎么办呢??
我之前已经有个办法了.那么我要去找一下第4章的兄弟帮忙

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

  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个点组成的线段对比另一个已知的点.

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

      • 先从网上找一下Statement expected, found Py:DEDENT
      • TAB还是空格呢??
      • 小小总结
      • 如何拆分矩形的四个点呢.
        • 我们来小小的测试一下这个函数
        • 结果
        • 出在哪里呢???
        • 修改完成
        • variable in function should be lowercase 函数变量应该小写

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

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

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

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

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

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

      • 先从网上找一下Statement expected, found Py:DEDENT
      • TAB还是空格呢??
      • 小小总结
      • 如何拆分矩形的四个点呢.
        • 我们来小小的测试一下这个函数
        • 结果
        • 出在哪里呢???
        • 修改完成
        • variable in function should be lowercase 函数变量应该小写

   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()
我们来小小的测试一下这个函数

def main():
    x1, y1, width1, height1 = eval(input("Enter x1,y1,width1,height1: "))
    r1 = Rectangle2D(x1, y1, width1, height1)
    r1.con_po()

main()
结果

在这里插入图片描述
别慌,我们把点标出来.

# 增加一段打印代码来看看这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的计算上.我们合计一下.

        # 我是不是不应该进行这样的减法
        rx4, ry4 =abs(rx1)-self.__height, ry3

修改完成
 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

      • 先从网上找一下Statement expected, found Py:DEDENT
      • TAB还是空格呢??
      • 小小总结
      • 如何拆分矩形的四个点呢.
        • 我们来小小的测试一下这个函数
        • 结果
        • 出在哪里呢???
        • 修改完成
        • variable in function should be lowercase 函数变量应该小写


    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")

七、本题最后的代码

      • 先从网上找一下Statement expected, found Py:DEDENT
      • TAB还是空格呢??
      • 小小总结
      • 如何拆分矩形的四个点呢.
        • 我们来小小的测试一下这个函数
        • 结果
        • 出在哪里呢???
        • 修改完成
        • variable in function should be lowercase 函数变量应该小写

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


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()

在这里插入图片描述

明天继续,加油通知们.


http://www.kler.cn/news/334028.html

相关文章:

  • 从Rally到Atlassian Cloud:思科Cisco实现云迁移的技术挑战与解决方案
  • 基于keras的停车场车位识别
  • Spring Boot新闻推荐系统:用户体验优化
  • LeetCode 1928.规定时间内到达终点的最小花费:动态规划
  • 【linux】基础IO(下)
  • Axios 和 Ajax的区别和联系
  • React 安装(NPM)
  • 基于SpringBoot+Vue的高校运动会管理系统
  • vSAN01:vSAN简介、安装、磁盘组、内部架构与调用关系
  • 第三十七章 结合加密和签名 - 安全标头元素的顺序
  • 国庆普及模拟赛-5
  • OpenGL ES 纹理(7)
  • k8s的学习和使用
  • Redis --- 第二讲 --- 特性和安装
  • CSDN 的 GIt 是没东西吗
  • 计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-04
  • 软考数据库部分 ---- (概念数据库模型,三级模式,两级映像,事物管理)
  • PostgreSQL Docker Error – 5432: 地址已被占用
  • 【nlp自然语言】知识图谱,全文检索,自然语言nlp,数据资产标签,集成管理平台
  • 如何使用ssm实现政务大厅管理系统+vue