Python math.hypot() Examples
The following are 30
code examples of math.hypot().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
math
, or try the search function
.
Example #1
Source File: box.py From pyx with GNU General Public License v2.0 | 6 votes |
def pointdistance_pt(self, x, y): result = None for p1, p2 in self.successivepoints(): gx, gy = p2[0] - p1[0], p2[1] - p1[1] if gx * gx + gy * gy < 1e-10: dx, dy = p1[0] - x, p1[1] - y else: a = (gx * (x - p1[0]) + gy * (y - p1[1])) / (gx * gx + gy * gy) if a < 0: dx, dy = p1[0] - x, p1[1] - y elif a > 1: dx, dy = p2[0] - x, p2[1] - y else: dx, dy = x - p1[0] - a * gx, y - p1[1] - a * gy new = math.hypot(dx, dy) if result is None or new < result: result = new return result
Example #2
Source File: normpath.py From pyx with GNU General Public License v2.0 | 6 votes |
def curvature_pt(self, params): result = [] # see notes in rotation approxarclen = (math.hypot(self.x1_pt-self.x0_pt, self.y1_pt-self.y0_pt) + math.hypot(self.x2_pt-self.x1_pt, self.y2_pt-self.y1_pt) + math.hypot(self.x3_pt-self.x2_pt, self.y3_pt-self.y2_pt)) for param in params: xdot = ( 3 * (1-param)*(1-param) * (-self.x0_pt + self.x1_pt) + 6 * (1-param)*param * (-self.x1_pt + self.x2_pt) + 3 * param*param * (-self.x2_pt + self.x3_pt) ) ydot = ( 3 * (1-param)*(1-param) * (-self.y0_pt + self.y1_pt) + 6 * (1-param)*param * (-self.y1_pt + self.y2_pt) + 3 * param*param * (-self.y2_pt + self.y3_pt) ) xddot = ( 6 * (1-param) * (self.x0_pt - 2*self.x1_pt + self.x2_pt) + 6 * param * (self.x1_pt - 2*self.x2_pt + self.x3_pt) ) yddot = ( 6 * (1-param) * (self.y0_pt - 2*self.y1_pt + self.y2_pt) + 6 * param * (self.y1_pt - 2*self.y2_pt + self.y3_pt) ) hypot = math.hypot(xdot, ydot) result.append((xdot*yddot - ydot*xddot) / hypot**3) return result
Example #3
Source File: utils.py From twod_materials with GNU General Public License v3.0 | 6 votes |
def get_markovian_path(points): """ Calculates the shortest path connecting an array of 2D points. Args: points (list): list/array of points of the format [[x_1, y_1, z_1], [x_2, y_2, z_2], ...] Returns: A sorted list of the points in order on the markovian path. """ def dist(x,y): return math.hypot(y[0] - x[0], y[1] - x[1]) paths = [p for p in it.permutations(points)] path_distances = [ sum(map(lambda x: dist(x[0], x[1]), zip(p[:-1], p[1:]))) for p in paths ] min_index = np.argmin(path_distances) return paths[min_index]
Example #4
Source File: planner.py From xy with MIT License | 6 votes |
def add_distances(self, indexes): n = len(self.distances) for i in indexes: if i < 0 or i >= n: continue j = i + 1 if self.reverse[i]: x1, y1 = self.paths[i][0] else: x1, y1 = self.paths[i][-1] if self.reverse[j]: x2, y2 = self.paths[j][-1] else: x2, y2 = self.paths[j][0] self.distances[i] = hypot(x2 - x1, y2 - y1) self.total_distance += self.distances[i]
Example #5
Source File: fft.py From respeaker_python_library with Apache License 2.0 | 6 votes |
def dft(self, data, typecode='h'): if type(data) is str: a = array.array(typecode, data) for index, value in enumerate(a): self.real_input[index] = float(value) elif type(data) is array.array: for index, value in enumerate(data): self.real_input[index] = float(value) self.fftwf_execute(self.fftwf_plan) for i in range(len(self.amplitude)): self.amplitude[i] = math.hypot(self.complex_output[i * 2], self.complex_output[i * 2 + 1]) # self.phase[i] = math.atan2(self.complex_output[i * 2 + 1], self.complex_output[i * 2]) return self.amplitude # , self.phase
Example #6
Source File: CircularRegion.py From OctoPrint-ExcludeRegionPlugin with GNU Affero General Public License v3.0 | 6 votes |
def containsRegion(self, otherRegion): """ Check if another region is fully contained in this region. Returns ------- True if the other region is fully contained inside this region, and False otherwise. """ from octoprint_excluderegion.RectangularRegion import RectangularRegion if (isinstance(otherRegion, RectangularRegion)): return ( self.containsPoint(otherRegion.x1, otherRegion.y1) and self.containsPoint(otherRegion.x2, otherRegion.y1) and self.containsPoint(otherRegion.x2, otherRegion.y2) and self.containsPoint(otherRegion.x1, otherRegion.y2) ) elif (isinstance(otherRegion, CircularRegion)): dist = math.hypot(self.cx - otherRegion.cx, self.cy - otherRegion.cy) + otherRegion.r return (dist <= self.r) else: raise ValueError("unexpected type: {otherRegion}".format(otherRegion=otherRegion))
Example #7
Source File: glyphObjects.py From Robofont-scripts with MIT License | 6 votes |
def constrainSegmentOffcurves(self, a1, h1, h2, a2): ax1, ay1 = a1 hx1, hy1 = h1 hx2, hy2 = h2 ax2, ay2 = a2 ix, iy = self.intersectLineLine(a1, h1, h2, a2) if (ix != None) and (iy != None): d1 = hypot(hx1-ax1, hy1-ay1) di1 = hypot(ix-ax1, iy-ay1) if d1 >= di1: t1 = atan2(hy1-ay1, hx1-ax1) hx1 = ax1 + 0.99*di1*cos(t1) hy1 = ay1 + 0.99*di1*sin(t1) d2 = hypot(hx2-ax2, hy2-ay2) di2 = hypot(ix-ax2, iy-ay2) if d2 >= di2: t2 = atan2(hy2-ay2, hx2-ax2) hx2 = ax2 + 0.99*di2*cos(t2) hy2 = ay2 + 0.99*di2*sin(t2) return (round(hx1), round(hy1)), (round(hx2), round(hy2))
Example #8
Source File: cpm_utils.py From convolutional-pose-machines-tensorflow with Apache License 2.0 | 6 votes |
def warpImage(src, theta, phi, gamma, scale, fovy): halfFovy = fovy * 0.5 d = math.hypot(src.shape[1], src.shape[0]) sideLength = scale * d / math.cos(deg2Rad(halfFovy)) sideLength = np.int32(sideLength) M = warpMatrix(src.shape[1], src.shape[0], theta, phi, gamma, scale, fovy) dst = cv2.warpPerspective(src, M, (sideLength, sideLength)) mid_x = mid_y = dst.shape[0] // 2 target_x = target_y = src.shape[0] // 2 offset = (target_x % 2) if len(dst.shape) == 3: dst = dst[mid_y - target_y:mid_y + target_y + offset, mid_x - target_x:mid_x + target_x + offset, :] else: dst = dst[mid_y - target_y:mid_y + target_y + offset, mid_x - target_x:mid_x + target_x + offset] return dst
Example #9
Source File: avoid.py From pg with MIT License | 6 votes |
def update(self, bots): px, py = self.position tx, ty = self.target angle = atan2(ty - py, tx - px) dx = cos(angle) dy = sin(angle) for bot in bots: if bot == self: continue x, y = bot.position d = hypot(px - x, py - y) ** 2 p = bot.padding ** 2 angle = atan2(py - y, px - x) dx += cos(angle) / d * p dy += sin(angle) / d * p angle = atan2(dy, dx) magnitude = hypot(dx, dy) self.angle = angle return angle, magnitude
Example #10
Source File: geometry.py From renpy-shader with MIT License | 6 votes |
def simplifyEdgePixels(pixels, minDistance): results = [] i = 0 while i < len(pixels): results.append((float(pixels[i][0]), float(pixels[i][1]))) distance = 0 i2 = i + 1 while i2 < len(pixels): previous = (pixels[i2 - 1][0], pixels[i2 - 1][1]) current = (pixels[i2][0], pixels[i2][1]) distance += math.hypot(current[0] - previous[0], current[1] - previous[1]) if distance > minDistance: break i2 += 1 i = i2 return results
Example #11
Source File: graph.py From pyx with GNU General Public License v2.0 | 5 votes |
def xvtickdirection(self, vx): if self.xorder: x1_pt, y1_pt = self.vpos_pt(vx, 1, 0) x2_pt, y2_pt = self.vpos_pt(vx, 0, 0) else: x1_pt, y1_pt = self.vpos_pt(vx, 0, 0) x2_pt, y2_pt = self.vpos_pt(vx, 1, 0) dx_pt = x2_pt - x1_pt dy_pt = y2_pt - y1_pt norm = math.hypot(dx_pt, dy_pt) return dx_pt/norm, dy_pt/norm
Example #12
Source File: pi_calc.py From scoop with GNU Lesser General Public License v3.0 | 5 votes |
def test(tries): return sum(hypot(random(), random()) < 1 for _ in range(tries)) # Calculates pi with a Monte-Carlo method. This function calls the function # test "n" times with an argument of "t". Scoop dispatches these # functions interactively accross the available ressources.
Example #13
Source File: graph.py From pyx with GNU General Public License v2.0 | 5 votes |
def yvtickdirection(self, vy): if self.yorder: x1_pt, y1_pt = self.vpos_pt(1, vy, 0) x2_pt, y2_pt = self.vpos_pt(0, vy, 0) else: x1_pt, y1_pt = self.vpos_pt(0, vy, 0) x2_pt, y2_pt = self.vpos_pt(1, vy, 0) dx_pt = x2_pt - x1_pt dy_pt = y2_pt - y1_pt norm = math.hypot(dx_pt, dy_pt) return dx_pt/norm, dy_pt/norm
Example #14
Source File: graph.py From pyx with GNU General Public License v2.0 | 5 votes |
def vtickdirection(self, vx1, vy1, vz1, vx2, vy2, vz2): x1_pt, y1_pt = self.vpos_pt(vx1, vy1, vz1) x2_pt, y2_pt = self.vpos_pt(vx2, vy2, vz2) dx_pt = x2_pt - x1_pt dy_pt = y2_pt - y1_pt norm = math.hypot(dx_pt, dy_pt) return dx_pt/norm, dy_pt/norm
Example #15
Source File: test_math.py From oss-ftp with MIT License | 5 votes |
def testHypot(self): self.assertRaises(TypeError, math.hypot) self.ftest('hypot(0,0)', math.hypot(0,0), 0) self.ftest('hypot(3,4)', math.hypot(3,4), 5) self.assertEqual(math.hypot(NAN, INF), INF) self.assertEqual(math.hypot(INF, NAN), INF) self.assertEqual(math.hypot(NAN, NINF), INF) self.assertEqual(math.hypot(NINF, NAN), INF) self.assertTrue(math.isnan(math.hypot(1.0, NAN))) self.assertTrue(math.isnan(math.hypot(NAN, -2.0)))
Example #16
Source File: smallestenclosingcircle.py From argoverse_baselinetracker with MIT License | 5 votes |
def make_diameter(p0, p1): cx = (p0[0] + p1[0]) / 2.0 cy = (p0[1] + p1[1]) / 2.0 r0 = math.hypot(cx - p0[0], cy - p0[1]) r1 = math.hypot(cx - p1[0], cy - p1[1]) return (cx, cy, max(r0, r1))
Example #17
Source File: maze.py From python-astar with BSD 3-Clause "New" or "Revised" License | 5 votes |
def heuristic_cost_estimate(self, n1, n2): """computes the 'direct' distance between two (x,y) tuples""" (x1, y1) = n1 (x2, y2) = n2 return math.hypot(x2 - x1, y2 - y1)
Example #18
Source File: spatial.py From levis with MIT License | 5 votes |
def euclidian(x1, y1, x2=None, y2=None): """Return the Euclidian distance between two points.""" if x2 is None and y2 is None: if len(x1) is 2 and len(y1) is 2: x2, y2 = y1 x1, y1 = x1 else: raise Exception("Not enough points given.") return math.hypot(x2 - x1, y2 - y1)
Example #19
Source File: smallestenclosingcircle.py From argoverse_baselinetracker with MIT License | 5 votes |
def make_circumcircle(p0, p1, p2): # Mathematical algorithm from Wikipedia: Circumscribed circle ax, ay = p0 bx, by = p1 cx, cy = p2 ox = (min(ax, bx, cx) + max(ax, bx, cx)) / 2.0 oy = (min(ay, by, cy) + max(ay, by, cy)) / 2.0 ax -= ox ay -= oy bx -= ox by -= oy cx -= ox cy -= oy d = (ax * (by - cy) + bx * (cy - ay) + cx * (ay - by)) * 2.0 if d == 0.0: return None x = ( ox + ( (ax * ax + ay * ay) * (by - cy) + (bx * bx + by * by) * (cy - ay) + (cx * cx + cy * cy) * (ay - by) ) / d ) y = ( oy + ( (ax * ax + ay * ay) * (cx - bx) + (bx * bx + by * by) * (ax - cx) + (cx * cx + cy * cy) * (bx - ax) ) / d ) ra = math.hypot(x - p0[0], y - p0[1]) rb = math.hypot(x - p1[0], y - p1[1]) rc = math.hypot(x - p2[0], y - p2[1]) return (x, y, max(ra, rb, rc))
Example #20
Source File: deformer.py From pyx with GNU General Public License v2.0 | 5 votes |
def _distchecked(self, orig_normcurve, parallel_normsubpath, epsilon, tstart, tend): # <<< """Helper routine for parallel.deformnicecurve: Checks the distances between orig_normcurve and parallel_normsubpath. The checking is done at parameters self.checkdistanceparams of orig_normcurve.""" dist = self.dist_pt # do not look closer than epsilon: dist_err = mathutils.sign(dist) * max(abs(self.relerr*dist), epsilon) checkdistanceparams = [tstart + (tend-tstart)*t for t in self.checkdistanceparams] for param, P, rotation in zip(checkdistanceparams, orig_normcurve.at_pt(checkdistanceparams), orig_normcurve.rotation(checkdistanceparams)): normal = rotation.apply_pt(0, 1) # create a short cutline for intersection only: cutline = normpath.normsubpath([normpath.normline_pt( P[0] + (dist - 2*dist_err) * normal[0], P[1] + (dist - 2*dist_err) * normal[1], P[0] + (dist + 2*dist_err) * normal[0], P[1] + (dist + 2*dist_err) * normal[1])], epsilon=epsilon) cutparams = parallel_normsubpath.intersect(cutline) distances = [math.hypot(P[0] - cutpoint[0], P[1] - cutpoint[1]) for cutpoint in cutline.at_pt(cutparams[1])] if (not distances) or (abs(min(distances) - abs(dist)) > abs(dist_err)): return False return True # >>>
Example #21
Source File: deformer.py From pyx with GNU General Public License v2.0 | 5 votes |
def _path_around_corner(self, corner_pt, beg_pt, end_pt, beg_tangent, end_tangent, is_concave, epsilon): # <<< """Helper routine for parallel.deformsubpath: Draws an arc around a convex corner""" if self.sharpoutercorners and not is_concave: # straight lines: t1, t2 = intersection(beg_pt, end_pt, beg_tangent, end_tangent) B = beg_pt[0] + t1 * beg_tangent[0], beg_pt[1] + t1 * beg_tangent[1] return normpath.normpath([normpath.normsubpath([ normpath.normline_pt(beg_pt[0], beg_pt[1], B[0], B[1]), normpath.normline_pt(B[0], B[1], end_pt[0], end_pt[1]) ])]) # We append an arc around the corner # these asserts fail in test case "E" #assert abs(math.hypot(beg_pt[1] - corner_pt[1], beg_pt[0] - corner_pt[0]) - abs(self.dist_pt)) < epsilon #assert abs(math.hypot(end_pt[1] - corner_pt[1], end_pt[0] - corner_pt[0]) - abs(self.dist_pt)) < epsilon angle1 = math.atan2(beg_pt[1] - corner_pt[1], beg_pt[0] - corner_pt[0]) angle2 = math.atan2(end_pt[1] - corner_pt[1], end_pt[0] - corner_pt[0]) # depending on the direction we have to use arc or arcn sinangle = beg_tangent[0]*end_tangent[1] - beg_tangent[1]*end_tangent[0] # >0 for left-turning, <0 for right-turning if self.dist_pt > 0: arcclass = path.arcn_pt else: arcclass = path.arc_pt return path.path(arcclass( corner_pt[0], corner_pt[1], abs(self.dist_pt), math.degrees(angle1), math.degrees(angle2))).normpath(epsilon=epsilon) # >>>
Example #22
Source File: deformer.py From pyx with GNU General Public License v2.0 | 5 votes |
def _length_pt(path, param1, param2): # <<< point1, point2 = path.at_pt([param1, param2]) return math.hypot(point1[0] - point2[0], point1[1] - point2[1]) # >>>
Example #23
Source File: deformer.py From pyx with GNU General Public License v2.0 | 5 votes |
def is_equiv(self, other, epsilon=None): """Test whether the two params yield essentially the same point""" assert self.normpath is other.normpath, "normpathparams have to belong to the same normpath" if self.normsubpathindex != other.normsubpathindex: return False nsp = self.normpath[self.normsubpathindex] if epsilon is None: epsilon = nsp.epsilon A, B = self.normpath.at_pt([self, other]) return math.hypot(A[0]-B[0], A[1]-B[1]) < epsilon
Example #24
Source File: deformer.py From pyx with GNU General Public License v2.0 | 5 votes |
def curvescontrols_from_endlines_pt(B, tangent1, tangent2, r1, r2, softness): # <<< # calculates the parameters for two bezier curves connecting two lines (curvature=0) # starting at B - r1*tangent1 # ending at B + r2*tangent2 # # Takes the corner B # and two tangent vectors heading to and from B # and two radii r1 and r2: # All arguments must be in Points # Returns the seven control points of the two bezier curves: # - start d1 # - control points g1 and f1 # - midpoint e # - control points f2 and g2 # - endpoint d2 # make direction vectors d1: from B to A # d2: from B to C d1 = -tangent1[0] / math.hypot(*tangent1), -tangent1[1] / math.hypot(*tangent1) d2 = tangent2[0] / math.hypot(*tangent2), tangent2[1] / math.hypot(*tangent2) # 0.3192 has turned out to be the maximum softness available # for straight lines ;-) f = 0.3192 * softness g = (15.0 * f + math.sqrt(-15.0*f*f + 24.0*f))/12.0 # make the control points of the two bezier curves f1 = B[0] + f * r1 * d1[0], B[1] + f * r1 * d1[1] f2 = B[0] + f * r2 * d2[0], B[1] + f * r2 * d2[1] g1 = B[0] + g * r1 * d1[0], B[1] + g * r1 * d1[1] g2 = B[0] + g * r2 * d2[0], B[1] + g * r2 * d2[1] d1 = B[0] + r1 * d1[0], B[1] + r1 * d1[1] d2 = B[0] + r2 * d2[0], B[1] + r2 * d2[1] e = 0.5 * (f1[0] + f2[0]), 0.5 * (f1[1] + f2[1]) return (d1, g1, f1, e, f2, g2, d2) # >>>
Example #25
Source File: deco.py From pyx with GNU General Public License v2.0 | 5 votes |
def _decocanvas(self, angle, dp, texrunner): dp.ensurenormpath() dist_pt = unit.topt(self.dist) c = canvas.canvas([canvas.clip(dp.path)]) llx_pt, lly_pt, urx_pt, ury_pt = dp.path.bbox().highrestuple_pt() center_pt = 0.5*(llx_pt+urx_pt), 0.5*(lly_pt+ury_pt) radius_pt = 0.5*math.hypot(urx_pt-llx_pt, ury_pt-lly_pt) + dist_pt n = int(2*radius_pt / dist_pt) + 1 for i in range(n): x_pt = center_pt[0] - radius_pt + i*dist_pt c.stroke(path.line_pt(x_pt, center_pt[1]-radius_pt, x_pt, center_pt[1]+radius_pt), [trafo.rotate_pt(angle, center_pt[0], center_pt[1])] + self.strokestyles) return c
Example #26
Source File: normpath.py From pyx with GNU General Public License v2.0 | 5 votes |
def arclen_pt(self, epsilon, upper=False): if upper: return self.l1_pt + self.l2_pt + self.l3_pt else: return math.hypot(self.x0_pt-self.x1_pt, self.y0_pt-self.y1_pt)
Example #27
Source File: avoid.py From pg with MIT License | 5 votes |
def update(self, dt): data = [bot.update(self.bots) for bot in self.bots] for bot, (angle, magnitude) in zip(self.bots, data): speed = min(1, 0.2 + magnitude * 0.8) dx = cos(angle) * dt * SPEED * bot.speed * speed dy = sin(angle) * dt * SPEED * bot.speed * speed px, py = bot.position tx, ty = bot.target bot.set_position((px + dx, py + dy)) if hypot(px - tx, py - ty) < 10: bot.target = self.select_point()
Example #28
Source File: normpath.py From pyx with GNU General Public License v2.0 | 5 votes |
def _arclentoparam_pt(self, lengths_pt, epsilon): # do self.arclen_pt inplace for performance reasons l_pt = math.hypot(self.x0_pt-self.x1_pt, self.y0_pt-self.y1_pt) return [length_pt/l_pt for length_pt in lengths_pt], l_pt
Example #29
Source File: nodes.py From VRPTW-ga with MIT License | 5 votes |
def get_distance_customers_pair(c1: Customer, c2: Customer) -> float: return math.hypot(c2.x - c1.x, c2.y - c1.y)
Example #30
Source File: fontList.py From fontgoggles with Apache License 2.0 | 5 votes |
def mouseDragged_(self, event): if (event.modifierFlags() & AppKit.NSEventModifierFlagCommand or event.modifierFlags() & AppKit.NSEventModifierFlagShift): return mx, my = self.mouseDownLocation x, y = self.convertPoint_fromView_(event.locationInWindow(), None) if math.hypot(x - mx, y - my) > 15: # Only do a drag beyond a minimum dragged distance self.mouseDownGlyphIndex = None super().mouseDragged_(event)