-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
184c76d
commit d2c2720
Showing
2 changed files
with
183 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { AbstractShape } from "./AbstractShape"; | ||
|
||
export type Point = [number, number]; | ||
|
||
export class Polygon extends AbstractShape { | ||
vertex: Point[] = []; | ||
convexHullVertex: Point[] = []; | ||
|
||
onCreate(width: number, height: number) { | ||
const size = width < height ? height : width; | ||
// hexagon | ||
this.vertex = [ | ||
[size / 2, 0], | ||
[size, size / 3], | ||
[size, (2 * size) / 3], | ||
[size / 2, size], | ||
[0, (2 * size) / 3], | ||
[0, size / 3], | ||
]; | ||
|
||
this.makeLocationArr() | ||
this.bufferLocSize = this.locationArr.length; | ||
this.type = "polygon"; | ||
this.scaleFactor = 0; | ||
|
||
|
||
this.color = [...v1, ...v2, ...v3, ...v3, ...v4, ...v1]; | ||
// 16 5 | ||
// 2 34 | ||
} | ||
drawShape(): void {} | ||
|
||
|
||
orientation(p: Point, q: Point, r: Point): number { | ||
const value = (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1]); | ||
if (value === 0) return 0; | ||
return value > 0 ? 1 : 2; | ||
} | ||
|
||
convexHull(points: Point[]): Point[] { | ||
const n = points.length; | ||
if (n < 3) return []; | ||
|
||
const hull: Point[] = []; | ||
|
||
// Find the leftmost point | ||
let leftmost = 0; | ||
for (let i = 1; i < n; i++) { | ||
if (points[i][0] < points[leftmost][0]) | ||
leftmost = i; | ||
} | ||
|
||
let p = leftmost; | ||
let q: number; | ||
do { | ||
hull.push(points[p]); | ||
q = (p + 1) % n; | ||
|
||
for (let i = 0; i < n; i++) { | ||
if (this.orientation(points[p], points[i], points[q]) === 2) | ||
q = i; | ||
} | ||
|
||
p = q; | ||
} while (p !== leftmost); | ||
|
||
return hull; | ||
} | ||
|
||
makeLocationArr() { | ||
this.convexHullVertex = this.convexHull(this.vertex) | ||
let newLoc : number[] = [] | ||
for (let i = 1; i < this.convexHullVertex.length - 2; i++) { | ||
newLoc = [...newLoc, ...this.vertex[0], ...this.vertex[i], ...this.vertex[i+1]] | ||
} | ||
this.locationArr = newLoc | ||
} | ||
|
||
makeColorArr() { | ||
let newColor: number[] = [] | ||
const v1 = [Math.random(), Math.random(), Math.random(), 1]; | ||
const v2 = [Math.random(), Math.random(), Math.random(), 1]; | ||
const v3 = [Math.random(), Math.random(), Math.random(), 1]; | ||
const v4 = [Math.random(), Math.random(), Math.random(), 1]; | ||
const v5 = [Math.random(), Math.random(), Math.random(), 1]; | ||
const v6 = [Math.random(), Math.random(), Math.random(), 1]; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,107 @@ | ||
import { AbstractShape } from "./AbstractShape"; | ||
|
||
export type Point = [number, number]; | ||
|
||
export class Polygon extends AbstractShape { | ||
onCreate(width: number, height: number) { | ||
const defSize = 200; | ||
const defLoc = [ | ||
width, height, | ||
width+defSize, height+defSize, | ||
width-defSize, height+defSize]; | ||
this.locationArr = defLoc; | ||
this.bufferLocSize = this.locationArr.length; | ||
this.type = "polygon"; | ||
this.scaleFactor = 0; | ||
const v1 = [0, 0, 0, 1]; | ||
const v2 = [0, 0, 0, 1]; | ||
const v3 = [0, 0, 0, 1]; | ||
this.color = [ | ||
...v1, | ||
...v2, | ||
...v3, | ||
] | ||
} | ||
vertex: Point[] = []; | ||
convexHullVertex: Point[] = []; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
onCreate(width: number, height: number) { | ||
// const defSize = 200; | ||
this.vertex = [ | ||
[width / 2, 0], | ||
[width, width / 3], | ||
[width, (2 * width) / 3], | ||
[width / 2, width], | ||
[0, (2 * width) / 3], | ||
[0, width / 3], | ||
]; | ||
|
||
this.makeLocationArr(); | ||
this.bufferLocSize = this.locationArr.length; | ||
this.type = "polygon"; | ||
this.scaleFactor = 0; | ||
const v = [0, 0, 0, 1]; | ||
this.color = [ | ||
...v, | ||
...v, | ||
...v, | ||
...v, | ||
...v, | ||
...v, | ||
...v, | ||
...v, | ||
...v, | ||
...v, | ||
...v, | ||
...v, | ||
]; | ||
} | ||
|
||
onAddVertex(x: number, y: number): void { | ||
console.log("add vertex", x, y); | ||
const inputLoc = [...this.locationArr, x, y]; | ||
const convexHull = this.convexHull(inputLoc) | ||
const data : number[] = [] | ||
for(let i=0;i<convexHull.length-2;i++){ | ||
data.push(convexHull[0][0],convexHull[0][1]) | ||
data.push(convexHull[i+1][0],convexHull[i+1][1]) | ||
data.push(convexHull[i+2][0],convexHull[i+2][1]) | ||
} | ||
this.locationArr = data | ||
this.bufferLocSize = this.locationArr.length | ||
const newColor = [] | ||
for (let i = 0; i < this.locationArr.length; i ++) { | ||
newColor.push(...[0, 0, 0, 1]); | ||
} | ||
console.log(newColor) | ||
console.log(this.locationArr) | ||
this.color = newColor; | ||
onAddVertex(x: number, y: number): void { | ||
console.log("add vertex", x, y); | ||
// const inputLoc = [...this.locationArr, x, y]; | ||
this.vertex.push([x, y]); | ||
// console.log(this.vertex) | ||
this.makeLocationArr() | ||
this.bufferLocSize = this.locationArr.length; | ||
const newColor = []; | ||
for (let i = 0; i < this.locationArr.length; i++) { | ||
newColor.push(...[0, 0, 0, 1]); | ||
} | ||
// console.log(newColor); | ||
// console.log(this.locationArr); | ||
this.color = newColor; | ||
} | ||
|
||
convexHull(inputLoc: number[]) : number[][] { | ||
const points: number[][] = [] | ||
for(let i=0;i<inputLoc.length;i+=2){ | ||
points.push([inputLoc[i],inputLoc[i+1]]) | ||
} | ||
orientation(p: Point, q: Point, r: Point): number { | ||
const value = (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1]); | ||
if (value === 0) return 0; | ||
return value > 0 ? 1 : 2; | ||
} | ||
|
||
const n = points.length; | ||
// There must be at least 3 points | ||
if(n<=3){ | ||
return points | ||
} | ||
convexHull(points: Point[]): Point[] { | ||
const n = points.length; | ||
if (n < 3) return []; | ||
|
||
const hull: number[][] = [] | ||
let l = 0; | ||
for(let i=1;i<n;i++){ | ||
if(points[i][0]<points[l][0]){ | ||
l = i | ||
} | ||
} | ||
const hull: Point[] = []; | ||
|
||
let p = l,q; | ||
do{ | ||
hull.push(points[p]) | ||
q = (p+1)%n | ||
for(let i=0;i<n;i++){ | ||
if(this.orientation(points[p],points[i],points[q])==2){ | ||
q = i | ||
} | ||
} | ||
p = q | ||
}while(p!=l) | ||
return hull | ||
// Find the leftmost point | ||
let leftmost = 0; | ||
for (let i = 1; i < n; i++) { | ||
if (points[i][0] < points[leftmost][0]) leftmost = i; | ||
} | ||
|
||
orientation(p: number[],q: number[],r: number[]): number { | ||
const val = (q[1]-p[1])*(r[2]-q[2])-(q[2]-p[2])*(r[1]-q[1]) | ||
if(val==0){ | ||
return 0 | ||
} | ||
return (val>0)?1:2 | ||
let p = leftmost; | ||
let q: number; | ||
do { | ||
hull.push(points[p]); | ||
q = (p + 1) % n; | ||
|
||
for (let i = 0; i < n; i++) { | ||
if (this.orientation(points[p], points[i], points[q]) === 2) q = i; | ||
} | ||
p = q; | ||
} while (p !== leftmost); | ||
|
||
return hull; | ||
} | ||
|
||
makeLocationArr() { | ||
this.convexHullVertex = this.convexHull(this.vertex); | ||
console.log(this.convexHullVertex); | ||
let newLoc: number[] = []; | ||
for (let i = 1; i < this.convexHullVertex.length - 1; i++) { | ||
newLoc = [ | ||
...newLoc, | ||
...this.convexHullVertex[0], | ||
...this.convexHullVertex[i], | ||
...this.convexHullVertex[i + 1], | ||
]; | ||
} | ||
drawShape(): void {} | ||
|
||
this.locationArr = newLoc; | ||
} | ||
|
||
drawShape(): void {} | ||
} |