2017-01-23 64 views
1

我有一個問題,這讓我瘋狂! :)我一直在日夜工作:) :)處理3 - PVector路徑偏移//向內/向外多邊形偏移

我的目標是什麼? I'm looking for a way for inward/outward polygon offsetting

在外面說2。 2裏面。使用Illustrator很容易:)

我的方法工作至今順時針

  1. 。獲取P1 P2 &
  2. 使用三角之間的角度來計算X & Y軸偏移
  3. 添加X & Y軸偏移到P1 P2 &。這是我如何獲得P1和P2之間的角度:
float getAngle = (atan((P1.y-P2.y)/(P1.x-p2.x))) * (180/PI) ; 

2.

// (COS(angle) = (adjacent side)/(hypotenuse)) || 2 = 6/3 
// (COS(angle) * (hypotenuse) = (adjacent side)  || 2 * 3 = 6 

// (SIN(angle) = (opposite side)/(hypotenuse)) || 2 = 6/3 
// (SIN(angle) * (hypotenuse) = (opposite side)  || 2 * 3 = 6 

我的問題

我想從你那裏得到什麼?

  • 是否有邏輯/公式來做到這一點?
  • 還是有一個圖書館,已經有這??

我只是不能圍繞我如何保持第一/中心線外的線偏移。

回答

0

你能否縮放頂點?

void setup(){ 
    size(400,400); 

    PVector[] originalPath = randomPath(7,100); 

    PVector[] insetPath = scalePoints(originalPath,0.75); 
    PVector[] outsetPath = scalePoints(originalPath,1.25); 

    background(255); 
    noFill(); 
    translate(width * .5, height * .5); 
    stroke(0,192,0); 
    drawPath(originalPath); 
    stroke(192,0,0); 
    drawPath(insetPath); 
    stroke(0,0,192); 
    drawPath(outsetPath); 

    fill(0); 
    text("original path",originalPath[0].x,originalPath[0].y); 
    text("inset path",insetPath[1].x,insetPath[1].y); 
    text("outset path",outsetPath[2].x,outsetPath[2].y); 
    text("click\nto\nreset",0,0); 
} 

void drawPath(PVector[] pts){ 
    beginShape(); 
    for(PVector p : pts) vertex(p.x,p.y); 
    endShape(CLOSE); 
} 

PVector[] scalePoints(PVector[] pts,float scale){ 
    int numPoints = pts.length; 
    PVector[] result = new PVector[numPoints]; 
    for(int i = 0 ; i < numPoints; i++){ 
    result[i] = pts[i].get(); 
    result[i].mult(scale); 
    } 
    return result; 
} 

PVector[] randomPath(int numPoints,float r){ 
    PVector[] result = new PVector[numPoints]; 
    float ai = TWO_PI/numPoints; 
    for(int i = 0 ; i < numPoints; i++){ 
    float radius = random(r-r*.25,r+r*.25); 
    result[i] = new PVector(cos(ai * i) * radius, sin(ai * i) * radius); 
    } 
    return result; 
} 

void mousePressed(){ 
    setup(); 
} 
void draw(){} 
+0

cr * p,非常抱歉的延誤。我不知道怎麼收到通知.. 不過謝謝!我要去看看它。看起來很有希望! – Tim