DDA Line Algorithm Implementation Guide

Aug 5, 2024

Lecture Notes: Implementing DDA Line Algorithm using QT Creator

Project Setup

  • Create a project named "DDA Line" in QT Creator (name can be customized).
  • Project files include:
    • dda.cpp
    • main.cpp
    • dda.h
    • ddaline.ui

UI Form Setup

  • Open the UI form to design the interface.
  • Input Fields: Line Segment Coordinates
    • Labels for X1, Y1, X2, Y2
    • Plain Text Edit Boxes for each coordinate
  • Push Button
    • Add a push button and rename it (e.g., "DDA").
    • Set up a click event for the button.

Setting Up Event Handlers

  • Connect Click Event to Function
    • Right-click the push button, go to slot, and select the clicked event.
    • Redirects to the main CPP file (dda.cpp or main.cpp), where the function will be defined.

Coding the DDA Function

  • Function Definition
    • void DDA(float x1, float y1, float x2, float y2)
    • Convert input values from text boxes to integers.
    • Example of converting text to integer:
      int x1 = ui->textEdit->toPlainText().toInt();
      
    • Retrieve values for X1, Y1, X2, Y2 similarly.
  • Drawing a Pixel
    • Example of setting a pixel: image.setPixel(50, 50, qRgb(255,255,255));
    • Use QLabel to display the result (label_5).

DDA Algorithm Implementation

  1. Variables
    • int x, y
    • float deltaX, deltaY, length, xIncrement, yIncrement
  2. Calculate Differences
    • deltaX = x2 - x1
    • deltaY = y2 - y1
  3. Determine Length
    • Use the greater of abs(deltaX) or abs(deltaY) to determine length
  4. Calculate Increments
    • xIncrement = deltaX / length
    • yIncrement = deltaY / length
  5. Loop to Draw Line
    • Initialize x and y with x1, y1
    • Use a loop from 0 to length
    • Plot pixels using image.setPixel(x, y, qRgb(r, g, b))
    • Increment x and y by xIncrement and yIncrement
    • Example loop:
      for (int i = 0; i < length; i++) {
          image.setPixel(x, y, qRgb(255, 255, 255));
          x += xIncrement;
          y += yIncrement;
      }
      

Running the Project

  • Build the project and ensure no errors.
  • Run the project.
  • Enter coordinates for the line segment (e.g., (100, 100) to (200, 200)).
  • Click the DDA button to draw the line on the output screen.

Additional Information

  • Output Screen
    • QLabel (label_5) used to display the drawn line.
    • Set QLabel size to 500x500.
  • Color Customization
    • Change RGB values in setPixel to alter line color.

Summary

  • Successfully implemented and ran the DDA line drawing algorithm in QT Creator.
  • Interface allows user to input coordinates and draw the corresponding line segment using the DDA algorithm.
  • Next steps could include implementing the Bresenham line algorithm similarly.