处理3 - PVector路径偏移//向内/向外多边形偏移

时间:2017-01-23 16:54:44

标签: coordinates processing polygon offset

我有一个问题让我疯了! :)我现在一直在日夜工作:):)

我的目标是什么? 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之间角度的方法:
  4. 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
    

    我的问题

    我想从你这里得到什么?

    • 是否有逻辑/公式来执行此操作?
    • 或者是否有一个已经有这个的图书馆?

    我无法绕过如何将线偏移保持在第一/中心线之外。

1 个答案:

答案 0 :(得分: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(){}
相关问题