5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

凸多角形の角を丸める

Last updated at Posted at 2015-12-15

Screen Shot 2015-12-15 at 15.49.47.png

任意の凸多角形の角を丸くするために使ったコードです。
頂点列が時計回りに格納されていることが条件です。

ofApp.cpp
void ofApp::getRoundedCornerPolygon(const vector<ofVec2f> &polygon, float r, ofPath &path) {
    path.clear();
    int n = polygon.size();
    for (int i = 0; i < n; i++) {
        // 線分AP, PBで作られる角を丸くする
        ofVec2f a = polygon[(i - 1 + n) % n];
        ofVec2f p = polygon[i];
        ofVec2f b = polygon[(i + 1) % n];
        
        // 角APBのなす角度
        float th = (b - p).angleRad(a - p);
        // 2辺に接する円の中心から頂点Pまでの距離
        float d = r * tan(PI / 2.0 - th / 2.0);
        // 線分APの長さ
        float paDist = a.distance(p);
        // AP上における円の接点C1
        ofVec2f c1 = a.getInterpolated(p, 1.0 - (d / paDist));
        // 円の中心C
        ofVec2f c = c1 + (p - a).getPerpendicular() * r;
        // 円弧の開始角度と終了角度
        float angleBegin = ofVec2f(1, 0).angle(p - a) - 90;
        float angleEnd = ofVec2f(1, 0).angle(b - p) - 90;
        
        if (i == 0) {
            path.moveTo(c1);
        } else {
            path.lineTo(c1);
        }
        path.arc(c, r, r, angleBegin, angleEnd);
    }
}
5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?