API

Draw a Sector of a Circle

Draw an Arc Between Two Vectors

Given an origin O and two vectors OA and OB, this snippet draws an arc between the two vectors with a given radius. The arc can be drawn in three ways: 0 (default) as a solid line, 1 as a dashed line, or 2 as a filled area between the arc and the vectors.

import { Matrix, Vector3 } from "@babylonjs/core/Maths/math.vector";
import { MeshBuilder } from "@babylonjs/core/Meshes/meshBuilder";
function showAngleSector(origin, vector1, vector2, radius, sectorType) {
radius = radius || 1;
sectorType = sectorType || 0;
const cross = Vector3.Cross(vector1, vector2);
const dot = Vector3.Dot(vector1, vector2);
const angle = Math.acos(dot / (vector1.length() * vector2.length()));
const points = [];
const minNb = 4;
const factor = 2;
let nbPoints = Math.floor(radius * angle * factor);
nbPoints = nbPoints < minNb ? minNb : nbPoints;
const firstPoint = Vector3.Normalize(vector1).scale(radius);
const lastPoint = Vector3.Normalize(vector2).scale(radius);
let matrix;
let ang = angle / nbPoints;
let rotated;
for (let i = 0; i < nbPoints; i++) {
matrix = Matrix.RotationAxis(cross, ang * i);
rotated = Vector3.TransformCoordinates(firstPoint, matrix);
points.push(rotated.add(origin));
}
points.push(lastPoint.add(origin));
let sector;
switch (sectorType) {
case 0:
sector = MeshBuilder.CreateLines("sector", { points }, scene);
break;
case 1:
sector = MeshBuilder.CreateDashedLines(\"sector\", { points: points, dashSize: 3, gapSize: 1, dashNb: nbPoints }, scene);
break;
case 2:
const pointO = [];
for (let j = 0; j < points.length; j++) {
pointO.push(origin);
}
sector = MeshBuilder.CreateRibbon(\"sector\", { pathArray: [points, pointO] }, scene);
break;
default:
sector = MeshBuilder.CreateLines("sector", { points }, scene);
break;
}
return sector;
}

Playground

Draw an Arc Between Two Vectors