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

《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.__xdef get_y(self):return self.__y# 实现另一个矩形的x,y坐标与当前矩形x,y对比def contains_points(self, other_x, other_y):a_t_valid = other_x - self.__xb_t_valid = other_y - self.__ya = (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.__yrx2, ry2 = abs(rx1) + self.__width, ry1rx3, ry3 = rx2, ry2 - self.__heightrx4, ry4 =abs(rx1)-self.__height, ry3turtle.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.__yrx2, ry2 = abs(rx1) + self.__width, ry1rx3, ry3 = rx2, ry2 - self.__heightrx4, ry4 = rx1, ry3print(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.__yrx2, ry2 = abs(rx1) + self.__width, ry1rx3, ry3 = rx2, ry2 - self.__heightrx4, ry4 = rx1, ry3judge_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 = xself.__y = yself.__width = widthself.__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.__xy_1 = other_rec.__ywidth_r = other_rec.__widthheight_r = other_rec.__heightturtle.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.__xdef get_y(self):return self.__y# 为了计算4对坐标设计def con_po(self):rx1, ry1 = self.__x - self.__width / 2, self.__height / 2 + self.__yrx2, ry2 = abs(rx1) + self.__width, ry1rx3, ry3 = rx2, ry2 - self.__heightrx4, ry4 = rx1, ry3print(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.__yrx2, ry2 = abs(rx1) + self.__width, ry1rx3, ry3 = rx2, ry2 - self.__heightrx4, ry4 = rx1, ry3judge_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.3x2, y2, width2, height2 = 1.3, 4.3, 4, 5.3r1 = 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.mrgr.cn/news/43552.html

相关文章:

  • 传感器模块编程实践(三)舵机+超声波模块融合DIY智能垃圾桶模型
  • 常见的基础系统
  • 今天学的Word小技巧——批量设置图片格式,批量让题注居中
  • 软考系统分析师知识点二:经济管理
  • 每日一道算法题——二分查找
  • clickhouse数据字典
  • 使用SpringBoot自定义注解+拦截器+token机制,实现接口的幂等性
  • 【go入门】流程控制语句
  • 51c视觉~CV~合集3
  • 基于Java的GeoTools对Shapefile文件属性信息深度解析
  • C语言进阶版第16课—自定义类型:结构体
  • webserver
  • 网络基础知识笔记(五)接口管理
  • 已解决-Nacos明明成功运行,但Spring报错连接不上
  • 《Linux从小白到高手》理论篇:一文概览常用Linux重要配置文件
  • 免费论文生成网站有哪些?推荐5款AI自动生成论文的网站
  • 基于SPI协议的Flash扇区擦除实验
  • 算法: 二分查找题目练习
  • Flex布局
  • Oracle数据库中表压缩的实现方式和特点