简体   繁体   English

涉及python类型更改的OOP问题的优雅解决方案

[英]Elegant Solution to an OOP Issue involving type-changing in python

I run into an OOP problem when coding something in python that I don't know how to address in an elegant solution. 我在python中编码某些我不知道如何解决优雅的解决方案时遇到了OOP问题。 I have a class that represents the equation of a line ( y = mx + b ) based on the m and b parameters, called Line . 我有一个基于mb参数(称为Line )代表线方程( y = mx + b )的类。 Vertical lines have infinite slope, and have equation x = c , so there is another class VerticalLine which only requires a c parameter. 垂直线具有无限的斜率,并且具有等式x = c ,因此还有另一类VerticalLine仅需要c参数。 Note that I am unable to have a Line class that is represented by two points in the xy-plane, if this were a solution I would indeed use it. 请注意,我无法拥有由xy平面中的两个点表示的Line类,如果这是一种解决方案,我确实会使用它。

I want to be able to rotate the lines. 我希望能够旋转线条。 Rotating a horizontal line by pi/2 + k*pi ( k an integer) results in a vertical line, and vice versa. 将水平线旋转pi/2 + k*pik为整数)会产生垂直线,反之亦然。 So a normal Line would have to somehow be converted to a VerticalLine in-place, which is impossible in python (well, not impossible but incredibly wonky). 因此,正常的Line必须以某种方式就地转换为VerticalLine ,这在python中是不可能的(嗯,不是不可能,但不可思议的是变幻莫测)。 How can I better structure my program to account for this problem? 如何更好地构建程序以解决此问题?

Note that other geometric objects in the program have a rotation method that is in-place, and they are already used frequently, so if I could I would like the line rotation methods to also be in place. 请注意,程序中的其他几何对象具有适当的旋转方法,并且它们已经被频繁使用,因此,如果我希望也可以使用直线旋转方法。 Indeed, this would be a trivial problem if the line rotation methods could return a new rotated Line or VerticalLine object as seen fit. 确实,如果线旋转方法可以返回新的旋转的LineVerticalLine对象(如合适),这将是一个小问题。

Geometric objects that have a fixed boundary/end-points can be translated and rotated in place. 具有固定边界/端点的几何对象可以平移和旋转。 But for a line, unless you talk about a line from point A to point B with a fixed length, you are looking at both end-points either being at infinity or -infinity ( y = mx + c ). 但是对于一条线,除非您谈论从点A到点B的线长固定,否则您将看到两个端点都位于infinity或- -infinityy = mx + c )。 Division using infinity or -infinity is not simple math and hence I believe complicates the rotation and translation algorithms 使用infinity或- -infinity infinity除法不是简单的数学运算,因此我相信旋转和平移算法会变得复杂

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM