Skip to content

Commit

Permalink
Refactor ellipse algorithm to allow for dynamic midpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
ifennna committed Apr 19, 2019
1 parent 95bb623 commit 6644937
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
54 changes: 28 additions & 26 deletions ellipses/ellipses.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,59 @@ package ellipses

import "go-graphics/pixel"

func Draw(start pixel.Point, majorDistance float64) {
minorDistance := start.Y
func Draw(midpoint pixel.Point, horizontalDistance float64, verticalDistance float64) {
var derivativeOfX float64 = 0
derivativeOfY := 2 * majorDistance * majorDistance * minorDistance
decisionVariable := (minorDistance * minorDistance) -
(majorDistance * majorDistance * minorDistance) +
(0.25 * majorDistance * majorDistance)
currentPixel := start
derivativeOfY := 2 * horizontalDistance * horizontalDistance * verticalDistance
decisionVariable := (verticalDistance * verticalDistance) -
(horizontalDistance * horizontalDistance * verticalDistance) +
(0.25 * horizontalDistance * horizontalDistance)
currentPixel := pixel.Point{X: 0, Y: verticalDistance}

for derivativeOfX < derivativeOfY {
drawMirroredPixels(currentPixel)
drawMirroredPixels(midpoint, currentPixel)
currentPixel.X++

derivativeOfX += minorDistance * minorDistance * 2
derivativeOfX += verticalDistance * verticalDistance * 2

if decisionVariable < 0 {
decisionVariable += derivativeOfX + (minorDistance * minorDistance)
decisionVariable += derivativeOfX + (verticalDistance * verticalDistance)
} else {
currentPixel.Y--
derivativeOfY -= majorDistance * majorDistance * 2
decisionVariable += derivativeOfX + (minorDistance * minorDistance) - derivativeOfY
derivativeOfY -= horizontalDistance * horizontalDistance * 2
decisionVariable += derivativeOfX + (verticalDistance * verticalDistance) - derivativeOfY
}
}

drawMirroredPixels(currentPixel)
drawMirroredPixels(midpoint, currentPixel)

decisionVariable = minorDistance*minorDistance*(currentPixel.X+0.5)*(currentPixel.X+0.5) +
majorDistance*majorDistance*(currentPixel.Y-1)*(currentPixel.Y-1) -
minorDistance*minorDistance*majorDistance*majorDistance
decisionVariable = verticalDistance*verticalDistance*(currentPixel.X+0.5)*(currentPixel.X+0.5) +
horizontalDistance*horizontalDistance*(currentPixel.Y-1)*(currentPixel.Y-1) -
verticalDistance*verticalDistance*horizontalDistance*horizontalDistance

for currentPixel.Y > 0 {
currentPixel.Y--
derivativeOfY -= majorDistance * majorDistance * 2
derivativeOfY -= horizontalDistance * horizontalDistance * 2

if decisionVariable >= 0 {
decisionVariable -= derivativeOfY + majorDistance*majorDistance
decisionVariable -= derivativeOfY + horizontalDistance*horizontalDistance
} else {
currentPixel.X++
derivativeOfX += minorDistance * minorDistance * 2
decisionVariable += derivativeOfX - derivativeOfY + majorDistance*majorDistance
derivativeOfX += verticalDistance * verticalDistance * 2
decisionVariable += derivativeOfX - derivativeOfY + horizontalDistance*horizontalDistance
}

drawMirroredPixels(currentPixel)
drawMirroredPixels(midpoint, currentPixel)
}
}

func drawMirroredPixels(point pixel.Point) {
sectorOne := point
sectorTwo := pixel.Point{X: -point.X, Y: point.Y}
sectorThree := pixel.Point{X: -point.X, Y: -point.Y}
sectorFour := pixel.Point{X: point.X, Y: -point.Y}
func drawMirroredPixels(midpoint pixel.Point, point pixel.Point) {
x := midpoint.X + point.X
y := midpoint.Y + point.Y

sectorOne := pixel.Point{X: x, Y: y}
sectorTwo := pixel.Point{X: -x, Y: y}
sectorThree := pixel.Point{X: -x, Y: -y}
sectorFour := pixel.Point{X: x, Y: -y}

pixel.DrawPixel(sectorOne)
pixel.DrawPixel(sectorTwo)
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func run() {
circles.Draw(pixel.Point{0, 0}, 19, "midpoint")
circles.Draw(pixel.Point{0, 0}, 29, "bresenhams")

ellipse := pixel.Point{0, 15}
ellipses.Draw(ellipse, 31)
ellipse := pixel.Point{0, 0}
ellipses.Draw(ellipse, 31, 15)

arcStart := pixel.Point{0, 70}
arcEnd := pixel.Point{50, 20}
Expand Down

0 comments on commit 6644937

Please sign in to comment.