-1

I have problem about implement midpoint algorithm with eight octant. The current situation is show in the picture below as y axis is inverted.

enter image description here

so far I manage to make zone 1 working prefect, I have no ideas how to make it work for other zone, I been google and all I find is zone 1 example not in other zone.

here my code for zone 1

void Line2D::MPOct1(const Window& window, const Point2D& start,
const Point2D& end, const float& delta_x, const float& delta_y) const
{
  Color color = mStartVtx.GetColor();

  int int_delta_x = static_cast<int>(round(delta_x));
  int int_delta_y = static_cast<int>(round(delta_y));

  int start_x = static_cast<int>(round(start.GetX()));
  int start_y = static_cast<int>(round(start.GetY()));
  int const end_x = static_cast<int>(round(end.GetX()));

  int f = 2 * int_delta_y - int_delta_x;
  int f_e = 2 * int_delta_y ;
  // should be se due to y axis inverted
  int f_ne = 2 * int_delta_y + 2 * int_delta_x;

  while (start_x <= end_x )
  {
      if (f >= 0)
      {
          window.SetPixel(start_x, start_y, color);
          f += f_e;
          start_x ++;
      }
      else
      {
          window.SetPixel(start_x, start_y, color);
          f += f_ne;
          start_x ++;
          // y axis inveted so --
          start_y --;
      }
  }

}

by default the decision parameter given is 2deltaY -deltaX and the 8 direction I assume is:

east = 2deltaY
west = -2deltaY
north = 2deltaX (-2deltaX due to inverse of y axis)
south = -2deltaX (2deltaX due to inverse of y axis)
north-east = 2deltaY -2deltaX (2deltaY +2deltaX due to inverse of y axis )
north-west = -2deltaY -2deltaX (-2deltaY +2deltaX due to inverse of y axis )
south-east = 2deltaY +2deltaX (2deltaY -2deltaX due to inverse of y axis )
south-west = -2deltaY +2deltaX (-2deltaY -2deltaX due to inverse of y axis )

anyone can tell me how solve other oct zone, by manipulate update I manage to make zone 1 ,4,5,and 8 work

Spektre
  • 49,595
  • 11
  • 110
  • 380

1 Answers1

0

1) Treat vertical and horizontal lines by special functions
2) Make two functions - F1 for the first octant (where x changes faster) and F2 for the second octant (where y changes faster)
3) Get absolute delta values like DeltaX = Abs(X2 - X1)
4) Get step direction like XStep = (X2 - X1>0)?1:-1
5) For 1,4,5,8 octants use function F1 with deltas and steps (instead start_x ++ make start_x += XStep etc)
6) For 2,3,6,7 octants use function F2, with deltas and steps

MBo
  • 77,366
  • 5
  • 53
  • 86