A rendering engine library for Go supporting both SDL2 and WebAssembly (HTML Canvas) targets.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.5 KiB

  1. package render
  2. // MidpointEllipse implements an ellipse plotting algorithm.
  3. func MidpointEllipse(center, radius Point) chan Point {
  4. yield := make(chan Point)
  5. go func() {
  6. var (
  7. pos = NewPoint(radius.X, 0)
  8. delta = NewPoint(
  9. 2*radius.Y*radius.Y*pos.X,
  10. 2*radius.X*radius.X*pos.Y,
  11. )
  12. err = radius.X*radius.X -
  13. radius.Y*radius.Y*radius.X +
  14. (radius.Y*radius.Y)/4
  15. )
  16. for delta.Y < delta.X {
  17. yield <- NewPoint(center.X+pos.X, center.Y+pos.Y)
  18. yield <- NewPoint(center.X+pos.X, center.Y-pos.Y)
  19. yield <- NewPoint(center.X-pos.X, center.Y+pos.Y)
  20. yield <- NewPoint(center.X-pos.X, center.Y-pos.Y)
  21. pos.Y++
  22. if err < 0 {
  23. delta.Y += 2 * radius.X * radius.X
  24. err += delta.Y + radius.X*radius.X
  25. } else {
  26. pos.X--
  27. delta.Y += 2 * radius.X * radius.X
  28. delta.X -= 2 * radius.Y * radius.Y
  29. err += delta.Y - delta.X + radius.X*radius.X
  30. }
  31. }
  32. err = radius.X*radius.X*(pos.Y*pos.Y+pos.Y) +
  33. radius.Y*radius.Y*(pos.X-1)*(pos.X-1) -
  34. radius.Y*radius.Y*radius.X*radius.X
  35. for pos.X >= 0 {
  36. yield <- NewPoint(center.X+pos.X, center.Y+pos.Y)
  37. yield <- NewPoint(center.X+pos.X, center.Y-pos.Y)
  38. yield <- NewPoint(center.X-pos.X, center.Y+pos.Y)
  39. yield <- NewPoint(center.X-pos.X, center.Y-pos.Y)
  40. pos.X--
  41. if err > 0 {
  42. delta.X -= 2 * radius.Y * radius.Y
  43. err += radius.Y*radius.Y - delta.X
  44. } else {
  45. pos.Y++
  46. delta.Y += 2 * radius.X * radius.X
  47. delta.X -= 2 * radius.Y * radius.Y
  48. err += delta.Y - delta.X + radius.Y*radius.Y
  49. }
  50. }
  51. close(yield)
  52. }()
  53. return yield
  54. }