本系列文章长期更新修改.
QLineF,是QLine的浮点数版本,但QLineF拥有的函数比QLine丰富得多,这主要是因为线条的很多操作都离不开浮点数.
属性:
Types
- enum IntersectType { NoIntersection, BoundedIntersection, UnboundedIntersection }
Methods
- __init__ (self, QLine line)
- __init__ (self)
- __init__ (self, QPointF apt1, QPointF apt2)
- __init__ (self, float x1pos, float y1pos, float x2pos, float y2pos)
- __init__ (self, QLineF other)
- float angle (self, QLineF l)
- float angle (self)
- float angleTo (self, QLineF l)
- float dx (self)
- float dy (self)
- IntersectType intersect (self, QLineF l, QPointF intersectionPoint)
- bool isNull (self)
- float length (self)
- QLineF normalVector (self)
- QPointF p1 (self)
- QPointF p2 (self)
- QPointF pointAt (self, float t)
- setAngle (self, float angle)
- setLength (self, float len)
- setLine (self, float aX1, float aY1, float aX2, float aY2)
- setP1 (self, QPointF aP1)
- setP2 (self, QPointF aP2)
- setPoints (self, QPointF aP1, QPointF aP2)
- QLine toLine (self)
- translate (self, QPointF point)
- translate (self, float adx, float ady)
- QLineF translated (self, QPointF p)
- QLineF translated (self, float adx, float ady)
- QLineF unitVector (self)
- float x1 (self)
- float x2 (self)
- float y1 (self)
- float y2 (self)
Static Methods
- QLineF fromPolar (float length, float angle)
Special Methods
- QLineF __mul__ (self, QMatrix m)
- QLineF __mul__ (self, QTransform m)
- bool __eq__ (self, QLineF d)
- bool __ne__ (self, QLineF d)
- int __nonzero__ (self)
- object __repr__ (self)
详细分析:
1.QLineF与QLine
虽然QLineF的函数比QLine丰富得多,但它们也有许多用法相同的函数,下面的函数用法都和QLine相同或相似,用法参考QLine篇.
- __init__ (self)
- __init__ (self, QPointF apt1, QPointF apt2)
- __init__ (self, float x1pos, float y1pos, float x2pos, float y2pos)
- __init__ (self, QLineF other)
- float x1 (self)
- float x2 (self)
- float y1 (self)
- float y2 (self)
- QPointF p1 (self)
- QPointF p2 (self)
- setP1 (self, QPointF aP1)
- setP2 (self, QPointF aP2)
- setLine (self, float aX1, float aY1, float aX2, float aY2)
- setPoints (self, QPointF aP1, QPointF aP2)
- translate (self, QPointF point)
- translate (self, float adx, float ady)
- QLineF translated (self, QPointF p)
- QLineF translated (self, float adx, float ady)
- float dx (self)
- float dy (self)
- bool isNull (self)
QLine和QLineF能互相转换,其中下面的toLine()会将浮点数属性四舍五入成整数属性.
- __init__ (self, QLine line)
- QLine toLine (self)
2.角度和长度
线段的表示,除了起点终点坐标表示法,还可以把终点替换成角度和长度.
但是,一般角度和长度都是浮点数的存在,因此只有QLineF才有这两个虚拟属性.
- float angle (self)
- setAngle (self, float angle)
- float length (self)
- setLength (self, float len)
注意,QLineF对角度和长度的改变,都是根据起点改变的.即起点不会变,只有终点会改变.
关于角度的方向,Qt仍然是以水平向右的线段为0度,线段逆时针转则角度不断增加.
角度的取值范围为[0,360),单位是角度,不是弧度.
3.判断线段相交
QLineF特有的IntersectType枚举类型,即为线段相交的几种情况.
枚举量 | 值 | 描述 |
---|---|---|
NoIntersection | 0 | 两线段平行 |
BoundedIntersection | 1 | 两线段相交 |
UnboundedIntersection | 2 | 两线段不相交,但所在直线相交 |
- IntersectType intersect (self, QLineF l, QPointF intersectionPoint)
4.线段计算
函数 | 含义 | 备注 |
---|---|---|
float angle (self, QLineF l) | 两线段夹角 | 取值范围[0,180) |
float angleTo (self, QLineF l) | 两线段夹角 | 该夹角为自身逆时针旋转到和参数线段水平的夹角,取值范围[0,360) |
QLineF normalVector (self) | 法向量 | 该法向量起点和长度和自身一样,角度是自身逆时针旋转90度的角度 |
QPointF pointAt (self, float t) | 比例点 | 求从起点开始延伸t*长度的位置,t取值范围[0,1]. t=0时即为起点,t=1时为终点 |
QLineF unitVector (self) | 单位向量 | 该单位向量起点和角度和自身一样,长度是1 |
5.从起点生成线段
fromPolar静态方法以(0,0)为起点,根据参数的角度和长度,生成一个QLineF对象.
- QLineF fromPolar (float length, float angle)