Algorithm for creating rounded corners in a polygon

前端 未结 5 701
醉酒成梦
醉酒成梦 2020-12-02 06:15

I\'m looking for an algorithm that allows me to create rounded corners from a polygon. In Input, I get an array of points that represents the polygon (red line) and in outpu

5条回答
  •  独厮守ぢ
    2020-12-02 06:34

    Here is a way using some geometry :-

    1. the two lines are tangent to circle inscribed
    2. The normal to the tangent meet at centre of the circle.
    3. Let angle between lines be X
    4. Angle subtended at centre of the circle will be K = 360-90*2-X = 180-X
    5. Lets decide the two point of tangents as (x1,y) and (x2,y)
    6. The chord joining the points has length l = (x2-x1)
    7. Inside the circle, the chord and two normals of length r (radius) form isosceles triangle
    8. The pendicular divide the traingle in to equal halved right angled triangles.
    9. One of angle is K/2 and side is l/2
    10. using properties of right angle triangle sin(K/2) = (l/2)/r
    11. r = (l/2)/sin(K/2)
    12. but K = 180-X so r = (l/2)/sin(90-X/2) = (l/2)/cos(X/2)
    13. hence r = (x2-x1)/(2*cos(X/2))
    14. Now simply draw a arc from (x1,y) to (x2,y) using radius r

    Note:-

    The above is explained for only for lines which meet at origin and the Y axis divides the angle between them into half. But it is equally applicable for all corner just need to apply a rotation and translation before applying the above. Moreover you need to select some x values of intersection from where you want to draw the arc . The values should not be too far or close to origin

提交回复
热议问题