---
title: Geometric Algorithms
source: https://vjmap.com/en/guide/geo.html
---

# Geometric Algorithms

## Point
|Class/Namespace|Method|Parameters|Description|
|:---|:---|:---|:---|
|GeoPoint|add|p: GeoPoint|Add two points. Example: let p1 = new vjmap.GeoPoint(1,2); let p2 = new vjmap.GeoPoint(3, 4); p1.add(p2); // 4, 6|
|GeoPoint|sub|p: GeoPoint|Subtract two points|
|GeoPoint|multByPoint|p: GeoPoint|Multiply two points|
|GeoPoint|divByPoint|p: GeoPoint|Divide two points|
|GeoPoint|dot|p: GeoPoint|Vector dot product|
|GeoPoint|lerp|v: GeoPoint, alpha: number|Interpolate a new point between two points by ratio|
|GeoPoint|rotate|angle: number|Rotate|
|GeoPoint|roateAround|angle: number, p: GeoPoint|Rotate around a point.|
|GeoPoint|matMult|m: number[]|Multiply by a matrix|
|GeoPoint|unit| |Unit length|
|GeoPoint|equals|other: GeoPoint, dotErr: number = 8|Check if equal|
|GeoPoint|distanceTo|p: GeoPoint|Distance between two points|
|GeoPoint|angle| |Angle|
|GeoPoint|angleTo|p: GeoPoint|Angle to a point|
|GeoPoint|angleWith|p: GeoPoint|interiorAngle to a point|
|GeoPoint|angleWithSep|p: GeoPoint|interiorAngle to a point|
|GeoPoint|transform|basePt: GeoPoint, destPt: GeoPoint, scale?:number, angle?: number|Scale, rotate, and translate a point around a base point.|

## Bounding Box
|Class/Namespace|Method|Parameters|Description|
|:---|:---|:---|:---|
|GeoBounds|center| |Get center point. Example: let bounds = new vjmap.GeoBounds([0,10], [10, 50]); let c = bounds.center(); // [5, 30]|
|GeoBounds|square|isLatlng?: boolean, isMinValue?: boolean|Make square with equal width and height||
|GeoBounds|width| |Get width||
|GeoBounds|height| |Get height||
|GeoBounds|scale|ratio: number, origin?: GeoPoint &#124; null|Scale to produce a new GeoBounds.||
|GeoBounds|scaleXY|ratioX: number, ratioY: number, origin?: GeoPoint &#124; null|Scale by X/Y ratio to produce a new GeoBounds.||
|GeoBounds|translate|dx: number, dy: number|Translate bounding box||
|GeoBounds|update|vertices: GeoPoint[]|Update bounding box||
|GeoBounds|updateByBounds|bounds: GeoBounds[] &#124; GeoBounds|Update bounding box from child bounds||
|GeoBounds|intersect|b: GeoBounds|Intersection of two bounding boxes||
|GeoBounds|union|bound: GeoBounds|Union of multiple bounding boxes||
|GeoBounds|isIntersect|bound: GeoBounds|Check if intersects with another bounding box||
|GeoBounds|isContains|bound: GeoBounds|Check if one bounding box contains another||
|GeoBounds|contains|point: GeoPoint|Check if point is inside bounding box||
|GeoBounds|closestPoint|testPoint: GeoPoint, includeInterior: boolean = true|Find the closest point on the bounding box and return it.||

## Line
|Class/Namespace|Method|Parameters|Description|
|:---|:---|:---|:---|
|vjmap|pointToSegmentDistance|p: GeoPoint, p1: GeoPoint, p2: GeoPoint|Distance from point to segment. Example: vjmap.pointToSegmentDistance(vjmap.geoPoint([3, 2]), vjmap.geoPoint([0,0]), vjmap.geoPoint([0, 10])) // 3 |
|vjmap|simplify|points: GeoPoint[], tolerance?: number|Simplify point coordinates||
|vjmap|closestPointOnSegment|p: GeoPoint, p1: GeoPoint, p2: GeoPoint|Closest point on segment to a point||
|vjmap|closestPointOnPolyline|p: GeoPoint, points: GeoPoint[]|Closest point on polyline to a point||
|vjmap|closestPointOnPolylines|p: GeoPoint, lines: GeoPoint[][]|Closest point on multiple lines to a point|
|vjmap|segmentIntersect|x1,y1,x2,y2,x3,y3,x4,y4|Segment intersection||
|vjmap|lineSplit|line: GeoPoint[], splitLine: GeoPoint[], dotErr?: number|Split a line by another line into multiple segments, returns new segments||
|vjmap|multiLineSplit|lines: GeoPoint[][], dotErr?: number|Split multiple lines by their intersections, returns all segments after splitting. Ensure each segment does not self-intersect||
|vjmap|clipSegment|a: GeoPoint, b: GeoPoint, bounds: GeoBounds|Clip segment by bounds||
|vjmap|interpolatePointsByRatio|points: GeoPointLike[], ratio: number, isLngLat: boolean = false|Get point coordinates from coordinate sequence by ratio (0-1)||
|vjmap|interpolateLineRange|points, number, isLngLat, offsetDist, minGap, includeSrcPoint|Vertices of (poly)line to interpolate (input must be lon/lat).||
|vjmap.Math2D|dist|ax,ay, bx, by|Calculate distance between two points| vjmap.Math2D.dist(10,10,10,30); // 20|
|vjmap.Math2D|lineDist|pts: GeoPointLike[]|Calculate line length| |
|vjmap.Math2D|distToSegmentSquared|px, py, l0x, l0y, l1x, l1y|Squared distance between point and 2D segment.| |
|vjmap.Math2D|intersectLineAndCircle|xLine1, yLine1, xLine2, yLine2, radius, xCenter, yCenter|Intersection of line and circle| |
|vjmap.Math2D|lineDist|pts: GeoPointLike[]|Calculate line length| |

## Polygon
|Class/Namespace|Method|Parameters|Description|
|:---|:---|:---|:---|
|vjmap|calcPolygonArea|points: GeoPoint[]|Calculate polygon area. Example: vjmap.calcPolygonArea([vjmap.geoPoint([0, 0]),vjmap.geoPoint([5, 5]),vjmap.geoPoint([10, 0])]) // 25|
|vjmap|clipPolygon|points: GeoPoint[], bounds: GeoBounds|Clip polygon by bounds||
|vjmap|bufferConvexPolygon|ring: GeoPoint[], buffer: number|Take a convex ring and expand it outward by applying a buffer. Assumes ring is clockwise wound.||
|vjmap|polygonIsClockwise|points: GeoPoint[]|Determine if polygon is clockwise using Green's formula| |
|vjmap|polygonCentroid|points: GeoPoint[]|Calculate polygon centroid from coordinate list| |
|vjmap|isPointInPolygon|pos: GeoPoint, polygon: GeoPoint[]|Check if point is inside polygon.| |
|vjmap|polylineMarginToPolygon|pts: GeoPointLike[],options|Expand polyline outward into a polygon.| |
|vjmap|createObjectPolygonUtil| | Create a polygon geometry utility object for difference, union, intersection, etc.| |

## Circle/Ellipse
|Class/Namespace|Method|Parameters|Description|
|:---|:---|:---|:---|
|vjmap|getCirclePolygonCoordinates|center,radius,points,startAngle,endAngle |Get discretized polygon points for circle or arc||
|vjmap|getCircleFeature|center,radius,points,startAngle,endAngle |Get discretized polygon as GeoJSON for circle or arc||
|vjmap|getEllipsePolygonCoordinates|Same as above, majorAxisRadius, minorAxisRadius |Get discretized polygon points for ellipse or elliptic arc||
|vjmap|getEllipseFeature|Same as above, majorAxisRadius, minorAxisRadius |Get discretized polygon as GeoJSON for ellipse or elliptic arc||

## Curve
|Class/Namespace|Method|Parameters|Description|
|:---|:---|:---|:---|
|vjmap|bezierCurveToPolyline|bezierCurve,precision, recursiveCount |Convert bezierCurve to polyline||
|vjmap|getBezierCurveLength|bezierCurve,precision, recursiveCount |Calculate Bezier curve length||
|vjmap|convertBezierCurveToPolyline|bezierCurve,precision, recursiveCount |Convert Bezier curve to polyline.||

## Topology Graph
|Class/Namespace|Method|Parameters|Description|
|:---|:---|:---|:---|
|vjmap|Graph| |Find shortest path. Example: var graph = Graph().addEdge("s", "t", 10).addEdge("s", "y", 5).addEdge("s", "y", 4).addEdge("t", "y", 2).addEdge("y", "t", 3).addEdge("t", "x", 1).addEdge("y", "x", 9).addEdge("y", "z", 2).addEdge("x", "z", 4).addEdge("z", "x", 6);var res = graph.shortestPath("s", "z")|
|vjmap|findShortestPath|startPoint, endPoint,lines,precision,hasDirection | Generate shortest path given all routes, start point, and end point| |

## Algorithms
|Class/Namespace|Method|Parameters|Description|
|:---|:---|:---|:---|
|vjmap|coordTransfromGetFourParamter|srcArr,destArr,isSetRotateZero,isConsiderPointOrder |Get four parameters for coordinate transformation| |
|vjmap|coordTransfromByFourParamter|pt: GeoPoint, param: {dx: number, dy: number, scale: number, rotate: number} |Coordinate transformation using four parameters| |
|vjmap|coordTransfromByInvFourParamter|pt: GeoPoint, param: {dx: number, dy: number, scale: number, rotate: number} |Coordinate transformation using inverse four parameters| |
|vjmap|buildTransformerMatrix3d|src: number[], dest: number[] |Perspective projection to 3D CSS matrix|  buildTransformerMatrix3d([10, 44, 100, 44, 100, 300, 10, 300],[50, 54, 130, 14, 140, 330, 14, 220]); // matrix3d(0.33941251980451626,-0.45559008266623746,0,-0.003618782566632648,-0.14489797252411882,0.32715165499980803,0,-0.0011983213450810846,0,0,1,0,48.53568735052134,39.35987390477654,0,1) |
|vjmap|vectorContour|featureCollection: FeatureCollection,weight: string, breaks: number[],params |Generate vector contour map| |

## Others
|Class/Namespace|Method|Parameters|Description|Example|
|:---|:---|:---|:---|:---|
|vjmap.MathUtils|equals|num1: number, num2: number, dotErr: number = 8 |Check if two numbers are equal| vjmap.MathUtils.equals(0.0041, 0.0042, 3) // true |
|vjmap.MathUtils|lerp|v1: number, v2: number, value: number |Linear interpolation|vjmap.MathUtils.lerp(10, 20, 0.3) // 13|
|vjmap.MathUtils|map|val: number, inMin: number, inMax: number, outMin: number, outMax: number|Map a number from one range to another.|vjmap.MathUtils.map(5, 0, 10, 100, 200) // 150|
|vjmap.MathUtils|clamp|value: number, min: number, max: number|Clamp value to given range.|vjmap.MathUtils.clamp(-1, 0, 10) // 0|
|vjmap.MathUtils|wrap|n: number, min: number, max: number, isMin: boolean = false|Constrain n to given range via modulo, excluding minimum|vjmap.MathUtils.wrap(11, 0, 10) // 1|

## Geometric Model

API documentation: [https://vjmap.com/docs/geomodel/](https://vjmap.com/docs/geomodel/)

Example code: [https://vjmap.com/demo/#/demo/map/models/01geomodels](https://vjmap.com/demo/#/demo/map/models/01geomodels)
