I have an array of CurveShapes and I’d like to round off the corners.
Maybe anyone already implemented something like this or has an algorithm for this?
Thanks for your help but I’m afraid you misunderstood me .
Maybe my description wasn’t very clear.
I was looking for something that turns straight lines into smooth bezier curves:
Meanwhile I found a solution on my own.
If anyone is interested:
Private Sub RoundCurves(elements() As CurveShape)
Const kPi As Double = 3.14159265
Const round As Double = 5.0
' --- Kurven abrunden ---
If elements = Nil Then Return
ReDim mPoints(-1)
Var lastAnchor1, lastAnchor2 As Pair
For i As Integer = 0 To elements.Ubound
' --- Abbrechen falls der übernächste Eintrag OOB wäre ---
If i + 1 > elements.Ubound Then Exit For
' --- Punkte definieren ---
Var curElement As CurveShape = elements(i)
Var nextElement As CurveShape = elements(i + 1)
' --- Winkel mit imaginärer Linie zu übernächstem Punkt berechnen ---
Var dirX As Double = nextElement.X2 - curElement.X
Var dirY As Double = nextElement.Y2 - curElement.Y
' --- Länge der Linie berechnen ---
Var distance As Double = Sqrt(Pow(dirX, 2) + Pow(dirY, 2))
' --- Unit-Vektor ---
Var unitX As Double = dirX / distance
Var unitY As Double = dirY / distance
' --- Normals ---
Var normal1 As Pair = -unitY : unitX
Var normal2 As PAir = unitY : -unitX
' --- Winkel für Ankerpunkte ---
Var angle1 As Double = ATan2(normal1.Right, normal1.Left) + kPi / 2
Var angle2 As Double = ATan2(normal2.Right, normal2.Left) + kPi / 2
' --- Ankerpunkte berechnen ---
Var anchor1X As Double = curElement.X2 + Cos(angle1) * (distance / round)
Var anchor1Y As Double = curElement.Y2 + Sin(angle1) * (distance / round)
Var anchor2X As Double = curElement.X2 + Cos(angle2) * (distance / round)
Var anchor2Y As Double = curElement.Y2 + Sin(angle2) * (distance / round)
Var anchor1 As Pair = anchor1X : anchor1Y
Var anchor2 As Pair = anchor2X : anchor2Y
' --- Ankerpunkte setzen ---
mPoints.Append anchor1
mPoints.Append anchor2
Select Case True
Case i = 0
curElement.ControlX(0) = curElement.X
curElement.ControlY(0) = curElement.Y
curElement.ControlX(1) = anchor1.Left
curElement.ControlY(1) = anchor1.Right
Case Else
curElement.ControlX(0) = lastAnchor2.Left
curElement.ControlY(0) = lastAnchor2.Right
curElement.ControlX(1) = anchor1.Left
curElement.ControlY(1) = anchor1.Right
' --- Ggf. letztes Element berücksichtigen ---
If i = elements.Ubound - 1 Then
nextElement.ControlX(0) = anchor2.Left
nextElement.ControlY(0) = anchor2.Right
nextElement.ControlX(1) = nextElement.X2
nextElement.ControlY(1) = nextElement.Y2
End If
End Select
' --- Letzte Ankerpunkte merken ---
lastAnchor1 = anchor1
lastAnchor2 = anchor2
Next
End Sub