three.cad/src/sketcher/drawArc.js

100 lines
2.6 KiB
JavaScript
Raw Normal View History

2021-03-25 16:04:13 +08:00
2021-03-27 04:40:40 +08:00
import * as THREE from '../../node_modules/three/src/Three';
2021-04-03 03:33:09 +08:00
import {lineMaterial, pointMaterial} from '../utils/shared'
2021-03-25 16:04:13 +08:00
2021-04-03 03:33:09 +08:00
import {ptObj, lineObj} from '../utils/shared'
2021-03-31 07:20:24 +08:00
const n = 30
2021-04-01 08:11:42 +08:00
export function drawArc(mouseLoc) {
2021-03-31 07:20:24 +08:00
const p1 = ptObj(mouseLoc)
2021-03-25 16:04:13 +08:00
p1.matrixAutoUpdate = false;
2021-03-31 16:07:34 +08:00
p1.userData.constraints = []
2021-03-31 07:20:24 +08:00
const p2 = ptObj()
2021-03-25 16:04:13 +08:00
p2.matrixAutoUpdate = false;
2021-03-31 16:07:34 +08:00
p2.userData.constraints = []
2021-03-25 16:04:13 +08:00
2021-03-31 07:20:24 +08:00
const arc = lineObj(n)
2021-03-25 16:04:13 +08:00
arc.frustumCulled = false;
2021-03-31 07:20:24 +08:00
const p3 = ptObj()
2021-03-31 16:07:34 +08:00
p3.matrixAutoUpdate = false;
p3.userData.constraints = []
2021-03-25 16:04:13 +08:00
return [p1, p2, p3, arc]
}
2021-04-01 08:11:42 +08:00
export function drawArc2(mouseLoc, toPush) {
2021-03-25 16:04:13 +08:00
const [p1, p2, p3, arc] = toPush
p2.geometry.attributes.position.set(mouseLoc);
p2.geometry.attributes.position.needsUpdate = true;
p2.geometry.computeBoundingSphere();
const [points, center] = get2PtArc(
p1.geometry.attributes.position.array,
p2.geometry.attributes.position.array
)
arc.geometry.attributes.position.set(
points
);
arc.geometry.attributes.position.needsUpdate = true;
p3.geometry.attributes.position.set(center);
p3.geometry.attributes.position.needsUpdate = true;
p3.geometry.computeBoundingSphere()
2021-03-26 12:30:35 +08:00
}
2021-03-31 07:20:24 +08:00
export function get2PtArc(p1, p2, divisions = n) {
2021-03-26 12:30:35 +08:00
const dx = p2[0] - p1[0]
const dy = p2[1] - p1[1]
const dist = Math.sqrt(dx ** 2 + dy ** 2)
const midAngle = (Math.atan2(dy, dx) - Math.PI / 2) % (2 * Math.PI)
let a1 = midAngle - Math.PI / 6
let a2 = midAngle + Math.PI / 6
a1 = a1 < 0 ? a1 + 2 * Math.PI : a1
a2 = a2 < 0 ? a2 + 2 * Math.PI : a1
const factor = Math.tan(Math.PI / 3)
const cx = (p1[0] + p2[0] - dy * factor) / 2
const cy = (p1[1] + p2[1] + dx * factor) / 2
const radius = dist
const deltaAngle = Math.PI / 3
let points = new Float32Array((divisions + 1) * 3)
for (let d = 0; d <= divisions; d++) {
const angle = a1 + (d / divisions) * deltaAngle;
points[3 * d] = cx + radius * Math.cos(angle);
points[3 * d + 1] = cy + radius * Math.sin(angle);
}
return [points, [cx, cy]];
}
2021-03-31 07:20:24 +08:00
export function get3PtArc(p1, p2, c, divisions = n) {
2021-03-26 12:30:35 +08:00
const v1 = [p1[0] - c[0], p1[1] - c[1]]
const v2 = [p2[0] - c[0], p2[1] - c[1]]
let a1 = Math.atan2(v1[1], v1[0])
let a2 = Math.atan2(v2[1], v2[0])
const radius = Math.sqrt(v1[0] ** 2 + v1[1] ** 2)
let deltaAngle = a2 - a1
2021-03-28 20:00:31 +08:00
if (deltaAngle <=0) deltaAngle += Math.PI*2
2021-03-26 12:30:35 +08:00
// console.log(deltaAngle)
2021-03-28 20:00:31 +08:00
2021-03-26 12:30:35 +08:00
let points = new Float32Array((divisions + 1) * 3)
for (let d = 0; d <= divisions; d++) {
const angle = a1 + (d / divisions) * deltaAngle;
points[3 * d] = c[0] + radius * Math.cos(angle);
points[3 * d + 1] = c[1] + radius * Math.sin(angle);
}
return points;
2021-03-25 16:04:13 +08:00
}