add geometry distance and angle function

This commit is contained in:
2019-08-28 16:28:19 +02:00
parent f47fa19cd2
commit b95f2d1bb0
2 changed files with 37 additions and 5 deletions

View File

@@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
"""2D geometry objects.
:Date: 2019-08-20
:Date: 2019-08-28
.. module:: geometry
:platform: *nix, Windows
@@ -14,6 +14,36 @@ import math
import numpy as np
def distance(point1, point2):
"""Distance between two points (or length of a straight line).
:param point1: first point (first end point of straight line)
:type point1: tuple
:param point2: second point (second end point of straight line)
:type point2: tuple
:returns: distance between the two points
:rtype: float
"""
return math.sqrt((point2[0]-point1[0])**2 + (point2[1]-point1[1])**2)
def angle(point1, point2=None):
"""Angle of point or between two points.
:param point1: (first) point
:type point1: tuple
:param point2: second point (default = None)
:type point2: tuple
:returns: angle of point or between two points
:rtype: float
"""
if point2 is None:
return math.atan2(point1[1], point1[0])
return math.atan2(point2[1]-point1[1], point2[0]-point1[0])
def translate(vec, *pts):
"""Translate a point or polygon by a given vector.
@@ -281,7 +311,7 @@ def cubics(pts, **kwargs):
if 'global_deformation' in kwargs:
lr = [(pts[l-kwargs['index_offset']],
pts[r-kwargs['index_offset']]) for l, r in kwargs['inc']]
ang = [math.atan2(r[1]-l[1], r[0]-l[0]) for l, r in lr]
ang = [angle(l, r) for l, r in lr]
# system deformation
U = kwargs['global_deformation']
if 'rotation_plane' in kwargs and 'xz' == kwargs['rotation_plane']:

View File

@@ -12,7 +12,9 @@
"""
import math
import pylab
from geometry import line, interpolate_hermite, rotate_xy, translate_xy
from geometry import (
distance, angle, line, interpolate_hermite, rotate_xy, translate_xy
)
pylab.style.use('dark_background')
pylab.rcParams['grid.color'] = 'gray'
@@ -74,8 +76,8 @@ def plot_cubic_lines(lns, **kwargs):
for ln in lns:
l, r, d = ln
L = math.sqrt((r[0]-l[0])**2 + (r[1]-l[1])**2)
ang = math.atan2(r[1]-l[1], r[0]-l[0])
L = distance(l, r)
ang = angle(l, r)
x, y = interpolate_hermite(d[1], d[2], d[4], d[5], d[0], d[3],
scale_x=L, scale_y=factor, samples=samples)
x, y = rotate_xy((0, 0), ang, x, y)