-
Notifications
You must be signed in to change notification settings - Fork 0
/
lineroutines.py
91 lines (80 loc) · 3.65 KB
/
lineroutines.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
"""***************************************************************************
* Copyright (C) 2009 by Derek Kite *
* dkite@shaw.ca *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************"""
import projectwizard
from PyQt4 import QtGui, QtCore
# line routines, intersections, etc.
class lineRoutines():
def __init__(self,scene):
self.scene = scene
def lineIntersect(self,initialpoint,directionpoint,linelist):
#targetline is qlinef that you want to check for intersections
#linelist is first element the origin, every element after the difference to the next point
targetline = QtCore.QLineF(QtCore.QPointF(initialpoint[0],initialpoint[1]),QtCore.QPointF(directionpoint[0],directionpoint[1]))
print linelist
print targetline
s = QtCore.QPointF(linelist[0][0],linelist[0][1])
e = QtCore.QPointF(linelist[0][0],linelist[0][1])
print e
print s
intersected = []
unbounded = []
for p in linelist:
e.setX(e.x()+p[0])
e.setY(e.y()+p[1])
line =QtCore.QLineF(e,s)
intersectionpoint = None
res = targetline.intersect(line,intersectionpoint)
if res == 1:
intersected.append(intersectionpoint)
if res == 2:
unbounded.append(intersectionpoint)
s.setX(e.x())
s.setY(e.y())
initialpointF = QtCore.QPointF(initialpoint[0],initialpoint[1])
closestpoint = None
if len(intersected) > 0:
for p in intersected:
if closestpoint == None:
closestpoint = p
continue
if QtCore.QLineF(initialpointF,closestpoint).length() > QtCore.QLineF(initialpointF,p).length():
if QtCore.QLineF(initialpointF,p).length() > 0:
closestpoint = p
if not closestpoint == None:
return [closestpoint.x(),closestpoint.y()]
else:
return None #initialpoint
def intersectLeft(self,point,linelist):
rect = self.scene.sceneRect()
dirpoint = [rect.x()-10,point[1]]
return self.lineIntersect(point,dirpoint,linelist)
def intersectRight(self,point,linelist):
rect = self.scene.sceneRect()
dirpoint = [rect.x()+rect.width()+10,point[1]]
return self.lineIntersect(point,dirpoint,linelist)
def intersectUp(self,point,linelist):
rect = self.scene.sceneRect()
dirpoint = [point[0],rect.y()-10]
return self.lineIntersect(point,dirpoint,linelist)
def intersectDown(self,point,linelist):
rect = self.scene.sceneRect()
dirpoint = [point[0],rect.y()+rect.height()+10]
return self.lineIntersect(point,dirpoint,linelist)