Introduction Lines Circles Sharp Waves Curly Waves Colors Lines Between Two Points Arcs Between Two Points

Arcs between Two Points - Maths317

An Arc is a broken circle

First the distance between the Two Points

Given the Points A (x1,y1) and B (x2,y2) , the distance formula is : D=(x2x1)2+(y2y1)2

Second the angle between the Two points

Using algebra with our line formula found in Lines for point x, we calculate the angle to be θ=cos1(x1x0)D

Formulas to get the center of the arc

Given diameter D to get r r=d2
x0=x1+r
y0=y1+r

Formulas for Start Angle and End Angle

θ1=atan2(y1y0,x1x0)
θ2=atan2(y2y0,x2x0)

After find this two values we can then use the method presented in circles for calculating the points for drawing

Test Code


package main

import (
  "github.com/disintegration/imaging"
  "image/color"
  "math"
  "math/rand"
  "time"
  "fmt"
)


func main() {
  img := imaging.New(500, 300, color.White)

  src := rand.NewSource(time.Now().UnixNano())
  seededRand := rand.New(src)

  x1 := seededRand.Intn(50)
  y1 := seededRand.Intn(50)

  x2 := seededRand.Intn(200)
  y2 := seededRand.Intn(200)

  fmt.Printf("A(%d, %d)\n", x1, y1)
  fmt.Printf("B(%d, %d)\n", x2, y2)
  diffX := x2 - x1
  diffY := y2 - y1
  tmp2 := math.Pow(float64(diffX), 2)
  tmp3 := math.Pow(float64(diffY), 2)
  distanceFromAtoBFloat := math.Sqrt(tmp2 + tmp3)
  distanceFromAtoB := int(math.Round(distanceFromAtoBFloat))

  fmt.Println("length is ", distanceFromAtoB)

  x0 := x1 + distanceFromAtoB/2
  y0 := y1 + distanceFromAtoB/2

  startAngle := (180/math.Pi * math.Atan2(float64(y1 - y0), float64(x1 - x0)))
  endAngle := (180/math.Pi * math.Atan2(float64(y2 - y0), float64(x2 - x0)))

  fmt.Println("start angle", startAngle)
  fmt.Println("end angle", endAngle)

  for angle := int(startAngle); angle < int(endAngle); angle++ {
    angleInRadians := float64(angle) * (math.Pi / 180)
    x := distanceFromAtoBFloat/2.0 * math.Sin(angleInRadians)
    y := distanceFromAtoBFloat/2.0 * math.Cos(angleInRadians)
    img.Set(x0 + int(x), y0 + int(y), color.Black)
  }

  err := imaging.Save(img, "arcs.png")
  if err != nil {
    panic(err)
  }
}

Note that the math functions in golang expects the angles in radians which was converted with the formula:

R=θpi180

References

  1. Ref 1
< Previous Next >