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 \((x_1, y_1)\) and B \((x_2, y_2)\) , the distance formula is : \( D = \sqrt{ (x_2 - x_1)^2 + (y_2 - y_1)^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 \( \theta = cos^{-1} { (x_1 - x_0) \over D } \)

Formulas to get the center of the arc

Given diameter D to get r \( r = {d \over 2} \)
\( x_0 = x_1 + r \)
\( y_0 = y_1 + r \)

Formulas for Start Angle and End Angle

\( \theta_1 = atan2 ( y_1 - y_0, x_1 - x_0) \)
\( \theta_2 = atan2 ( y_2 - y_0, x_2 - x_0) \)

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 = \theta {pi \over 180} \)

References

  1. Ref 1
< Previous Next >