Add a method to the Canvas class that draws a straight line between two arbitrary points using Bresenham's line algorithm. This is a fundamental rasterization technique that determines which pixels best approximate a perfect line. The method will internally call setPixel for each calculated point, allowing smooth and precise line drawing regardless of slope.
1.No new files – modify existing Canvas.h and Canvas.cpp.
2.Method signature:
Add to Canvas the public method:
void drawLine(int x1, int y1, int x2, int y2, const Color& color);
3.Algorithm requirements:
Implement the classic Bresenham line algorithm for integer coordinates.
The algorithm must handle all cases: shallow and steep slopes, lines drawn right‑to‑left or bottom‑to‑top.
Do not use floating‑point arithmetic; the algorithm should only use integer addition, subtraction, and comparison.
Use setPixel to draw each pixel of the line. You may assume that the setPixel method already performs boundary checks.
4.Edge cases:
If the start and end points are identical, a single pixel should be drawn.
The line should include both endpoints.
No explicit handling is needed for coordinates outside the canvas because setPixel already ignores out‑of‑bounds calls.
5.Implementation notes (optional but helpful):
A typical Bresenham implementation calculates dx = abs(x2 - x1), dy = -abs(y2 - y1).
It then decides between an x‑driving or y‑driving loop based on whether dx >= abs(dy).
Use setPixel at each step inside the loop.
Ensure the code compiles with the current Canvas class without breaking existing functionality.
Criteria for successful completion
- The method drawLine is declared in Canvas.h and implemented in Canvas.cpp.
- The code compiles without errors or warnings.
- A straight diagonal line appears continuous (no gaps) when inspected pixel by pixel.
- Horizontal and vertical lines are drawn correctly.
- Lines drawn in reversed order (e.g., from right to left) produce exactly the same set of pixels as the forward direction.
- The algorithm uses only integer arithmetic (no float or double).
- Existing functionality of Canvas (constructor, setPixel, getPixel) remains unchanged.
Tips
- Review the Bresenham algorithm explanation online; many resources show pseudo‑code.
- Pay attention to the sign of dy and the error term to correctly choose the next pixel.
- Write clear comments in the code explaining the steps.
- Test boundary cases: line from (0,0) to (0,0) should draw exactly one pixel.
- Because setPixel already checks bounds, you don't need to clip the line.
- This is the first algorithm-heavy task; take your time to understand it thoroughly – it will be reused for future shapes.
Add a method to the Canvas class that draws a straight line between two arbitrary points using Bresenham's line algorithm. This is a fundamental rasterization technique that determines which pixels best approximate a perfect line. The method will internally call setPixel for each calculated point, allowing smooth and precise line drawing regardless of slope.
Criteria for successful completion
Tips