-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCadIntersection.py
90 lines (73 loc) · 3 KB
/
CadIntersection.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
CadInput
A QGIS plugin
Provides CAD-like input globally : digitize features with precise numerical input for the angle, the distance, and easily make widCuctions lines
-------------------
begin : 2014-02-05
copyright : (C) 2014 by Olivier Dalang
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
from qgis.core import QgsPoint
from math import sqrt
class CadIntersection():
@staticmethod
def LineIntersectionAtX(linePoint1, linePoint2, x):
x1 = linePoint1.x()
y1 = linePoint1.y()
x2 = linePoint2.x()
y2 = linePoint2.y()
dx = x2 - x1
dy = y2 - y1
if dx==0:
y = y1
else:
y = y1+(dy * (x-x1) ) / dx
return y
@staticmethod
def LineIntersectionAtY(linePoint1, linePoint2, y):
x1 = linePoint1.x()
y1 = linePoint1.y()
x2 = linePoint2.x()
y2 = linePoint2.y()
dx = x2 - x1
dy = y2 - y1
if dy==0:
x = x1
else:
x = x1+(dx * (y-y1) ) / dy
return x
@staticmethod
def CircleLineIntersection(linePoint1, linePoint2, circleCenter, r):
# formula taken from http://mathworld.wolfram.com/Circle-LineIntersection.html
x1 = linePoint1.x()-circleCenter.x()
y1 = linePoint1.y()-circleCenter.y()
x2 = linePoint2.x()-circleCenter.x()
y2 = linePoint2.y()-circleCenter.y()
dx = x2-x1
dy = y2-y1
dr = sqrt(dx**2+dy**2)
d = x1*y2-x2*y1
def sgn(x): return -1 if x<0 else 1
DISC = r**2 * dr**2 - d**2
if DISC<=0:
#no intersection or tangeant
return None, None
else:
#first possible point
ax = circleCenter.x() + (d*dy+sgn(dy)*dx*sqrt(r**2*dr**2-d**2))/(dr**2)
ay = circleCenter.y() + (-d*dx+abs(dy)*sqrt(r**2*dr**2-d**2))/(dr**2)
#second possible point
bx = circleCenter.x() + (d*dy-sgn(dy)*dx*sqrt(r**2*dr**2-d**2))/(dr**2)
by = circleCenter.y() + (-d*dx-abs(dy)*sqrt(r**2*dr**2-d**2))/(dr**2)
return QgsPoint(ax,ay), QgsPoint(bx,by)