计算机图形学的程序实验c,[C]计算机图形学实验三

Cohen-Sutherland裁剪算法

df33a417fc45da7dd4fbf631bd61e909.png

运行图

e97c4092fc9bc8b494264b6433899676.png

// Exp3.cpp : 定义应用程序的入口点。

//

#include "stdafx.h"

#include "Exp3.h"

#include "math.h"

#define MAX_LOADSTRING 100

#define Array_Num  25 //点的数目

#define LEFT 1

#define RIGHT 2

#define TOP 8

#define BOTTOM 4

// 全局变量:

HINSTANCE hInst;        // 当前实例

TCHAR szTitle[MAX_LOADSTRING];     // 标题栏文本

TCHAR szWindowClass[MAX_LOADSTRING];   // 主窗口类名

// 此代码模块中包含的函数的前向声明:

ATOM    MyRegisterClass(HINSTANCE hInstance);

BOOL    InitInstance(HINSTANCE, int);

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int CohenSutherlandClip(HDC hdc,double x1,double y1,double x2,double y2);//Cohen-Sutherland裁剪算法

int Encode(double x,double y);//编码

int setClipRect(int left,int right,int top,int bottom);//设置裁剪矩形

int DrawGraphics(HDC hdc);//画图函数

int flag=0;//标志位

int XL,XR,YT,YB;//左右上下

int APIENTRY _tWinMain(HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPTSTR    lpCmdLine,

int       nCmdShow)

{

UNREFERENCED_PARAMETER(hPrevInstance);

UNREFERENCED_PARAMETER(lpCmdLine);

// TODO: 在此放置代码。

MSG msg;

HACCEL hAccelTable;

// 初始化全局字符串

LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);

LoadString(hInstance, IDC_EXP3, szWindowClass, MAX_LOADSTRING);

MyRegisterClass(hInstance);

// 执行应用程序初始化:

if (!InitInstance (hInstance, nCmdShow))

{

return FALSE;

}

hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_EXP3));

// 主消息循环:

while (GetMessage(&msg, NULL, 0, 0))

{

if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

}

}

return (int) msg.wParam;

}

//

//  函数: MyRegisterClass()

//

//  目的: 注册窗口类。

//

//  注释:

//

//    仅当希望

//    此代码与添加到 Windows 95 中的“RegisterClassEx”

//    函数之前的 Win32 系统兼容时,才需要此函数及其用法。调用此函数十分重要,

//    这样应用程序就可以获得关联的

//    “格式正确的”小图标。

//

ATOM MyRegisterClass(HINSTANCE hInstance)

{

WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style   = CS_HREDRAW | CS_VREDRAW;

wcex.lpfnWndProc = WndProc;

wcex.cbClsExtra  = 0;

wcex.cbWndExtra  = 0;

wcex.hInstance  = hInstance;

wcex.hIcon   = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_EXP3));

wcex.hCursor  = LoadCursor(NULL, IDC_ARROW);

wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);

wcex.lpszMenuName = MAKEINTRESOURCE(IDC_EXP3);

wcex.lpszClassName = szWindowClass;

wcex.hIconSm  = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

return RegisterClassEx(&wcex);

}

//

//   函数: InitInstance(HINSTANCE, int)

//

//   目的: 保存实例句柄并创建主窗口

//

//   注释:

//

//        在此函数中,我们在全局变量中保存实例句柄并

//        创建和显示主程序窗口。

//

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)

{

HWND hWnd;

hInst = hInstance; // 将实例句柄存储在全局变量中

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,

CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)

{

return FALSE;

}

ShowWindow(hWnd, nCmdShow);

UpdateWindow(hWnd);

return TRUE;

}

//

//  函数: WndProc(HWND, UINT, WPARAM, LPARAM)

//

//  目的: 处理主窗口的消息。

//

//  WM_COMMAND - 处理应用程序菜单

//  WM_PAINT - 绘制主窗口

//  WM_DESTROY - 发送退出消息并返回

//

//

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

int wmId, wmEvent;

PAINTSTRUCT ps;

HDC hdc;

RECT rect;

GetClientRect(hWnd,&rect);

//裁剪窗口参数

int left=400;//左

int right=600;//右

int top=250;//上

int bottom=50;//下

switch (message)

{

case WM_COMMAND:

wmId    = LOWORD(wParam);

wmEvent = HIWORD(wParam);

// 分析菜单选择:

switch (wmId)

{

case IDM_ABOUT:

DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);

break;

case IDM_EXIT:

DestroyWindow(hWnd);

break;

case ID_Tailor:

InvalidateRect(hWnd,&rect,true);

flag=1;

break;

default:

return DefWindowProc(hWnd, message, wParam, lParam);

}

break;

case WM_PAINT:

hdc = BeginPaint(hWnd, &ps);

// TODO: 在此添加任意绘图代码...

SetTextColor(hdc,RGB(0,0,0));//设置字体颜色为黑色

TextOutW(hdc,0,0,_T("计算机图形学实验3 E3-1---裁剪---CohenSutherland算法"),39);

setClipRect(left,right,top,bottom);//设置截取窗口的左右上下坐标

MoveToEx(hdc,left,bottom,NULL);

LineTo(hdc,right,bottom);

LineTo(hdc,right,top);

LineTo(hdc,left,top);

LineTo(hdc,left,bottom);

DrawGraphics(hdc);

EndPaint(hWnd, &ps);

break;

case WM_DESTROY:

PostQuitMessage(0);

break;

default:

return DefWindowProc(hWnd, message, wParam, lParam);

}

return 0;

}

// “关于”框的消息处理程序。

INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)

{

UNREFERENCED_PARAMETER(lParam);

switch (message)

{

case WM_INITDIALOG:

return (INT_PTR)TRUE;

case WM_COMMAND:

if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)

{

EndDialog(hDlg, LOWORD(wParam));

return (INT_PTR)TRUE;

}

break;

}

return (INT_PTR)FALSE;

}

//画图函数

int DrawGraphics(HDC hdc)

{

if (flag==1)

{

HPEN hPen;        //画笔声明

hPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0)); //红色画笔

SelectObject(hdc,hPen);//设置画笔颜色

double x0=400,y0=250;//平移坐标

double x[Array_Num],y[Array_Num];//坐标数组

double t=3.14*2/Array_Num;//t为角度(弧度制)

double radius=200;//半径

for(int i=0;i

{

x[i]=radius*cos(i*t)+x0;

y[i]=radius*sin(i*t)+y0;

}

for (int i=0;i

{

for (int j=i+1;j

{

CohenSutherlandClip(hdc,x[i],y[i],x[j],y[j]);

}

}

DeleteObject(hPen);

}

else

{

}

return 0;

}

//设置裁剪矩形

int setClipRect(int left,int right,int top,int bottom)

{

XL=left;

XR=right;

YT=top;

YB=bottom;

return 0;

}

//CohenSutherland截取线段

int CohenSutherlandClip(HDC hdc,double x1,double y1,double x2,double y2)

{

int code1,code2,code;

double x,y;

code1=Encode(x1,y1);

code2=Encode(x2,y2);

while (code1!=0||code2!=0)

{

if ((code1&code2)!=0)

{

return 0;

}

code=code1;

if (code1==0)

{

code=code2;

}

if((LEFT&code)!=0)

{

x=XL;

y=y1+(y2-y1)*(XL-x1)/(x2-x1);

}

if ((RIGHT&code)!=0)

{

x=XR;

y=y1+(y2-y1)*(XR-x1)/(x2-x1);

}

if ((TOP&code)!=0)

{

y=YT;

x=x1+(x2-x1)*(YT-y1)/(y2-y1);

}

if ((BOTTOM&code)!=0)

{

y=YB;

x=x1+(x2-x1)*(YB-y1)/(y2-y1);

}

if (code==code1)

{

x1=x;

y1=y;

code1=Encode(x,y);

}

else

{

x2=x;

y2=y;

code2=Encode(x,y);

}

}

MoveToEx(hdc,x1,y1,NULL);

LineTo(hdc,x2,y2);

return 0;

}

//编码函数

int Encode(double x,double y)

{

int c=0;

if (x

{

c=c|LEFT;

}

if(x>XR)

{

c=c|RIGHT;

}

if (y

{

c=c|BOTTOM;

}

if(y>YT)

{

c=c|TOP;

}

return c;

}

附:一种任意多边形的裁剪算法 http://good.gd/1823216.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值