Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

Point Clipping Algorithm ใน Computer Graphics ด้วย C++

Computer graphics จัดการกับการวาดภาพและกราฟิกส์บนหน้าจอคอมพิวเตอร์ ที่นี่ เราจัดการกับหน้าจอเป็นระบบพิกัด 2 มิติ ระบบพิกัดนี้เริ่มจากมุมซ้ายบน (0,0) และสิ้นสุดที่มุมขวาล่าง

Viewing plane (ระนาบมุมมอง) คือพื้นที่ที่กำหนดสำหรับการวาดกราฟิกส์ใน computer graphics หรือพื้นที่ที่มองเห็นได้ของหน้าจอ

Clipping คือการลบจุดหรือกราฟิกส์ที่อยู่นอก viewing plane

มาดูตัวอย่างเพื่อเข้าใจ clipping:

Point Clipping Algorithm ใน Computer Graphics ด้วย C++

ที่นี่จุด C และ D จะถูก clipped เนื่องจากอยู่นอก viewing plane ที่ทำเครื่องหมายด้วยสีน้ำเงิน

เพื่อ clip จุดใน computer graphics เราต้องรู้พิกัดของ viewing plane คือ (Xmin, Ymin) และ (Xmax, Ymax) จากนั้นเราจะเปรียบเทียบพิกัดของจุดกับพิกัดเหล่านี้

หาก (Xmin, Ymin) <= (Xpoint, Ypoint) <= (Xmax, Ymax) จุดจะอยู่ภายใน viewing plane มิฉะนั้นจะถูก clipped off

ตัวอย่าง

โปรแกรมแสดง point clipping:

#include <iostream>
using namespace std;

void pointClipping(int points[][2], int n, int Xmin, int Ymin, int Xmax, int Ymax) {
    cout << "Points that are removed by Point clipping Algorithm are :" << endl;
    for (int i = 0; i < n; i++) {
        if ((points[i][0] < Xmin) || (points[i][0] > Xmax))
            cout << "(" << points[i][0] << "," << points[i][1] << ")\t";
        else if ((points[i][1] < Ymin) || (points[i][1] > Ymax))
            cout << "(" << points[i][0] << "," << points[i][1] << ")\t";
    }
}

int main() {
    int points[6][2] = {{0, 0}, {-10, 10}, {1000, 1000}, {100, 900}, {501, 311}, {250, 250}};
    int Xmin = 0;
    int Xmax = 500;
    int Ymin = 0;
    int Ymax = 500;
    pointClipping(points, 6, Xmin, Ymin, Xmax, Ymax);
    return 0;
}

ผลลัพธ์

Points that are removed by Point clipping Algorithm are : (-10,10) (1000,1000) (100,900) (501,311)