円・扇型
code: geometry.ts
/**
* 円クラス
*/
export class Circle {
/**
* 中心座標
*/
center: Point;
/**
* 半径
*/
radius: number;
/**
* 中心座標と半径から円を生成します
* @param center 中心座標
* @param radius 半径
*/
constructor(center: Point, radius: number) {
this.center = center;
this.radius = radius;
}
/**
* 3点を通る円を生成します
* @param p1
* @param p2
* @param p3
*/
static createFor3points(p1: Point, p2: Point, p3: Point) {
let dist1 = p1.norm();
let dist2 = p2.norm();
let dist3 = p3.norm();
let u =
0.5 /
(p1.x * p2.y -
p2.x * p1.y +
p2.x * p3.y -
p3.x * p2.y +
p3.x * p1.y -
p1.x * p3.y);
let center = new Point(0, 0);
center.x =
u *
(dist1 * p2.y -
dist2 * p1.y +
dist2 * p3.y -
dist3 * p2.y +
dist3 * p1.y -
dist1 * p3.y);
center.y =
u *
(p1.x * dist2 -
p2.x * dist1 +
p2.x * dist3 -
p3.x * dist2 +
p3.x * dist1 -
p1.x * dist3);
let r = center.minus(p1).abs();
return new Circle(center, r);
}
/**
* 2点と半径から決まる円のリストを返します
* @param p1
* @param p2
* @param r
*/
static createFor2pointsAndRadius(p1: Point, p2: Point, r: number) {
let m = p1.midPoint(p2);
let vec = p2.minus(p1);
let len = vec.abs();
if (len > 2 * r + EPS) {
return [];
}
if (len.eq(2 * r)) {
}
let x = Math.sqrt(r * r - (len / 2) * (len / 2));
let c1 = vec.mult(Pt(0, x / vec.abs())).plus(m);
let c2 = vec.mult(Pt(0, -x / vec.abs())).plus(m);
let circles = [];
circles.push(new Circle(c1, r));
circles.push(new Circle(c2, r));
return circles;
}
/**
* 引数から円を生成します
* @param obj1
* @param obj2
* @param obj3
*/
static create(obj1: Point, obj2: Point, obj3: Point | number) {
if (
obj1 instanceof Point &&
obj2 instanceof Point &&
obj3 instanceof Point
) {
return Circle.createFor3points(obj1, obj2, obj3);
}
if (
obj1 instanceof Point &&
obj2 instanceof Point &&
typeof obj3 === "number"
) {
return Circle.createFor2pointsAndRadius(obj1, obj2, obj3);
}
return null;
}
/**
* 点が円に含まれるかを判定します
* @param point
*/
contain(point: Point) {
return point.minus(this.center).abs() < this.radius;
}
}
扇型クラス
code: geometry.ts
/**
* 扇型クラス
*/
export class Sector {
center: Point;
radius: number;
startAngle: number;
angleSize: number;
anticlockwise: boolean;
constructor(
center: Point,
radius: number,
startAngle: number,
angleSize: number,
anticlockwise: boolean
) {
this.center = center;
this.radius = radius;
this.startAngle = startAngle;
this.angleSize = angleSize;
if (anticlockwise == null) {
anticlockwise = true;
}
this.anticlockwise = anticlockwise;
}
}