mohan-bee/stm32-midi-keys
Clips rectangular ground-region geometry to fit within the board’s scaled rounded-edge outline using polygon intersection.
- Version
- 1.0.2
- License
- unset
- Stars
- 0
clip-ground-outline.ts
PCB
Schematic
type Point = {x:number,y:number}
/** Clip the source's rectangular ground regions inside the rounded board edge. */
export function clipGroundOutline(subject:Point[], board:Point[]):Point[] {
const clip=board.map(p=>({x:p.x*169.4/170,y:p.y*59.4/60}))
const cross=(a:Point,b:Point,p:Point)=>(b.x-a.x)*(p.y-a.y)-(b.y-a.y)*(p.x-a.x)
const area=clip.reduce((sum,p,i)=>sum+p.x*clip[(i+1)%clip.length].y-clip[(i+1)%clip.length].x*p.y,0)
const sign=Math.sign(area)
let output=subject
for(let i=0;i<clip.length;i++) {
const a=clip[i],b=clip[(i+1)%clip.length], input=output
output=[]
if(!input.length) break
for(let j=0;j<input.length;j++) {
const p=input[j],q=input[(j+1)%input.length]
const dp=sign*cross(a,b,p),dq=sign*cross(a,b,q)
if(dp>=-1e-9) output.push(p)
if((dp>=0)!==(dq>=0)) {const t=dp/(dp-dq);output.push({x:p.x+t*(q.x-p.x),y:p.y+t*(q.y-p.y)})}
}
}
return output
}