-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelipses.py
56 lines (42 loc) · 1.94 KB
/
elipses.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
#source: https://gis.stackexchange.com/questions/243459/drawing-ellipse-with-shapely
from matplotlib import pyplot
from shapely.geometry.point import Point
import shapely.affinity
from descartes import PolygonPatch
# Note: download figures.py manually from shapely github repo, put it in shapely install directory
from figures import SIZE, GREEN, GRAY, set_limits
# 1st elem = center point (x,y) coordinates
# 2nd elem = the two semi-axis values (along x, along y)
# 3rd elem = angle in degrees between x-axis of the Cartesian base
# and the corresponding semi-axis
ellipse = ((0, 0),(7, 4),36)
# Let create a circle of radius 1 around center point:
circ = shapely.geometry.Point(ellipse[0]).buffer(1)
# Let create the ellipse along x and y:
ell = shapely.affinity.scale(circ, int(ellipse[1][0]), int(ellipse[1][1]))
# Let rotate the ellipse (clockwise, x axis pointing right):
ellr = shapely.affinity.rotate(ell,ellipse[2])
# If one need to rotate it clockwise along an upward pointing x axis:
elrv = shapely.affinity.rotate(ell,90-ellipse[2])
# According to the man, a positive value means a anti-clockwise angle,
# and a negative one a clockwise angle.
ellipse2 = ((-4, -4),(7, 4),140)
circ2 = shapely.geometry.Point(ellipse2[0]).buffer(1)
ell2 = shapely.affinity.scale(circ2, int(ellipse2[1][0]), int(ellipse2[1][1]))
ellr2 = shapely.affinity.rotate(ell2,ellipse2[2])
elrv2 = shapely.affinity.rotate(ell2,90-ellipse2[2])
inter = elrv.intersection(elrv2)
fig = pyplot.figure()
fig2 = pyplot.figure()
ax = fig.add_subplot(111)
ax2 = fig2.add_subplot(111)
patch = PolygonPatch(elrv, fc=GREEN, ec=GRAY, alpha=0.5, zorder=2)
patch2 = PolygonPatch(elrv2, fc=GREEN, ec=GRAY, alpha=0.5, zorder=2)
ax.add_patch(patch)
ax.add_patch(patch2)
interPatch = PolygonPatch(inter, fc=GREEN, ec=GRAY, alpha=0.5, zorder=2)
ax2.add_patch(interPatch)
set_limits(ax, -10, 10, -10, 10)
set_limits(ax2, -10, 10, -10, 10)
fig.savefig('elipses.png')
fig2.savefig('intersection.png')