C++ 中點線生成演算法


一條線連線兩點。它是圖形的基本元素。要繪製一條線,您需要兩點,您可以在螢幕上這兩點之間繪製一條線,在圖形方面,我們將這些點稱為畫素,每個畫素都與整數座標相關聯。我們以 (x1, y1) 和 (x2, y2) 的形式給出整數座標,其中 x1 < x2 且 y1 < y2。任務是使用中點線生成演算法計算點 1 即 (x1, y1) 和點 2 即 (x2, y2) 之間的所有中點。

有三種不同的演算法用於在螢幕上執行線生成,它們是:

  • DDA 演算法

  • Bresenham 線生成演算法

  • 中點演算法

中點演算法

使用中點線演算法繪製線條的步驟:

  • 使用當前定位點計算中點,即東點 (Xp+1, Yp) 和東北點 (Xp+1, Yp+1) 是中點 (Xp+1, Yp+1/2)。

  • 現在,中點將決定螢幕上下一個座標的位置,即:

    • 如果中點在直線上方,則下一個座標將在東側。

    • 如果中點在直線下方,則下一個座標將在東北側。

讓我們看看這種情況的各種輸入輸出場景:

輸入 - int x_1 = 3, int y_1 = 3, int x_2 = 10, int y_2 = 8

輸出 - 線生成演算法的中點是:3,3 4,4 5,5 6,5 7,6 8,7 9,7 10,8

說明 - 我們給出的座標為 x_1 = 3, x_2 = 10, y_1 = 3, y_2 = 8。因此,步驟首先是計算 dx = x_2 - x_1 為 10 - 3 = 7,dy 為 y_2 - y_1 為 8 - 3 = 5,然後檢查 dy 是否小於 dx。現在計算 d 為 5 - (7 / 2) = 2。第一個點將是 x_1 和 y_1。列印它們。現在,當 x_1 < x_2 時,繼續將 x_1 增加 1,並檢查 d 是否小於 0,如果是,則將 d 設定為 d + dy,否則,將 d 設定為 d + (dy - dx) 並將 x_2 增加 1。

輸入 - int x_1 = 2, int y_1 = 2, int x_2 = 3, int y_2 = 4

輸出 - 線生成演算法的中點是:2,2 3,3 3,4

說明 - 我們給出的座標為 x_1 = 2, x_2 = 2, y_1 = 3, y_2 = 4。因此,透過應用中點線生成演算法,我們將計算所有中點畫素作為輸出。

下面程式中使用的方法如下:

  • 將整數點作為輸入 int x_1, int y_1, int x_2, int y_2。呼叫函式 Mid_Point(x_1, y_1, x_2, y_2) 來生成直線。

  • 在函式 Mid_Point(x_1, y_1, x_2, y_2) 內部

    • 計算 dx 為 x_2 - x_1,dy 為 y_2 - y_1

    • 檢查 IF dy 小於或等於 dx,則將 d 設定為 dy - (dx / 2),並將 first_pt 設定為 x_1,並將 second_pt 設定為 y_1

    • 列印 first_pt 和 second_pt。

    • 啟動 while first_pt 小於 x_2,然後將 first_pt 加 1,並檢查 IF d 小於 0,則將 d 設定為 d + dy,否則,將 d 設定為 d + (dy - dx) 並將 second_pt 加 1。列印 first_pt 和 second_pt。

    • 否則,如果 dx 小於 dy,則將 d 設定為 dx - (dy/2),並將 first_pt 設定為 x_1,並將 second_pt 設定為 y_1 並列印 first_pt 和 second_pt。

    • 啟動 WHILE second_pt 小於 y_2。在 WHILE 內部,將 second_pt 加 1。檢查 IF d 小於 0,則將 d 設定為 d + dx。否則,將 d 設定為 d + (dx - dy) 並將 first_pt 加 1。

    • 列印 first_pt 和 second_pt。

示例

#include<bits/stdc++.h>
using namespace std;

void Mid_Point(int x_1, int y_1, int x_2, int y_2){
   int dx = x_2 - x_1;
   int dy = y_2 - y_1;

   if(dy <= dx){
      int d = dy - (dx / 2);
      int first_pt = x_1;
      int second_pt = y_1;

      cout<< first_pt << "," << second_pt << "\n";
      while(first_pt < x_2){
         first_pt++;
         if(d < 0){
            d = d + dy;
         }
         else{
            d = d + (dy - dx);
            second_pt++;
         }
            cout << first_pt << "," << second_pt << "\n";
      }
   }
   else if(dx < dy){
      int d = dx - (dy/2);
      int first_pt = x_1;
      int second_pt = y_1;
      cout << first_pt << "," << second_pt << "\n";
      while(second_pt < y_2){
         second_pt++;
         if(d < 0){
            d = d + dx;
         }
         else{
            d += (dx - dy);
            first_pt++;
         }
         cout << first_pt << "," << second_pt << "\n";
      }
   }
}
int main(){
   int x_1 = 3;
   int y_1 = 3;
   int x_2 = 10;
   int y_2 = 8;
   cout<<"Mid-Points through Line Generation Algorithm are: ";
   Mid_Point(x_1, y_1, x_2, y_2);
   return 0;
}

輸出

如果我們執行以上程式碼,它將生成以下輸出

Mid-Points through Line Generation Algorithm are: 3,3
4,4
5,5
6,5
7,6
8,7
9,7
10,8

更新於:2021年10月22日

3K+ 次瀏覽

啟動您的 職業生涯

完成課程獲得認證

開始學習
廣告