NEFU计算机图形学实验二

2024-04-28 06:12

本文主要是介绍NEFU计算机图形学实验二,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

区域填充:根据种子填充算法的基本原理,编写图形填充的应用程序,并运行演示填充效果。

// hiView.cpp : implementation of the CHiView class
//#include "stdafx.h"
#include "hi.h"
#include <windows.h>
#include "hiDoc.h"
#include "hiView.h"
#include <afxtempl.h>#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/
// CHiViewvoid EdgeMark(int x1,int y1,int x2,int y2,int bcolor,CDC*pDC)
{int x;float k, y = y1;k=1.0*(y2-y1)/(x2-x1);for(x=x1; x<=x2; x++){pDC->SetPixel(x,(int)(y+0.5),bcolor);y=y+k;}
}
void ZhongZiTC4(int seedx, int seedy, int fcolor, int bcolor, CDC* pDC)
{CArray<CPoint, CPoint> stack;stack.Add(CPoint(seedx, seedy));while (stack.GetSize()>0){CPoint current = stack.GetAt(stack.GetUpperBound());stack.RemoveAt(stack.GetUpperBound());int x = current.x;int y = current.y;if (pDC->GetPixel(x, y) != bcolor && pDC->GetPixel(x, y) != fcolor){pDC->SetPixel(x, y, fcolor);stack.Add(CPoint(x, y + 1));stack.Add(CPoint(x, y - 1));stack.Add(CPoint(x - 1, y));stack.Add(CPoint(x + 1, y));}}
}
void SeedFill4 (int cnt,POINT*pts,int seedx,int seedy,int fcolor,int bcolor,CDC*pDC)
{int i; POINT v1,v2;for(i=0; i<cnt-1; i++){v1=pts[i]; v2=pts[i+1];EdgeMark(v1.x,v1.y,v2.x,v2.y,bcolor,pDC);}ZhongZiTC4 (seedx,seedy,fcolor,bcolor,pDC);
}IMPLEMENT_DYNCREATE(CHiView, CView)BEGIN_MESSAGE_MAP(CHiView, CView)//{{AFX_MSG_MAP(CHiView)// NOTE - the ClassWizard will add and remove mapping macros here.//    DO NOT EDIT what you see in these blocks of generated code!//}}AFX_MSG_MAP// Standard printing commandsON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()/
// CHiView construction/destructionCHiView::CHiView()
{// TODO: add construction code here}CHiView::~CHiView()
{
}BOOL CHiView::PreCreateWindow(CREATESTRUCT& cs)
{// TODO: Modify the Window class or styles here by modifying//  the CREATESTRUCT csreturn CView::PreCreateWindow(cs);
}/
// CHiView drawingvoid CHiView::OnDraw(CDC* pDC)
{// 设置画笔颜色CPen pen(PS_SOLID, 1, RGB(0, 0, 255));pDC->SelectObject(&pen);// 绘制矩形pDC->Rectangle(25, 25, 100, 75);// 设置填充颜色CBrush brush(RGB(139, 0, 0));pDC->SelectObject(&brush);// 设置种子点和填充颜色int seedx = 50, seedy = 50;int fcolor = RGB(139, 0, 0), bcolor = RGB(0, 0, 255);// 填充种子点ZhongZiTC4(seedx, seedy, fcolor, bcolor, pDC);
}void CHiView::ZhongZiTC4(int seedx, int seedy, COLORREF fcolor, COLORREF bcolor, CDC* pDC)
{CArray<CPoint, CPoint> stack;stack.Add(CPoint(seedx, seedy));while (stack.GetSize() > 0){CPoint current = stack.GetAt(stack.GetUpperBound());stack.RemoveAt(stack.GetUpperBound());int x = current.x;int y = current.y;if (pDC->GetPixel(x, y) != bcolor && pDC->GetPixel(x, y) != fcolor){pDC->SetPixel(x, y, fcolor);stack.Add(CPoint(x, y + 1));stack.Add(CPoint(x, y - 1));stack.Add(CPoint(x - 1, y));stack.Add(CPoint(x + 1, y));}}
}/
// CHiView printingBOOL CHiView::OnPreparePrinting(CPrintInfo* pInfo)
{// default preparationreturn DoPreparePrinting(pInfo);
}void CHiView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{// TODO: add extra initialization before printing
}void CHiView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{// TODO: add cleanup after printing
}/
// CHiView diagnostics#ifdef _DEBUG
void CHiView::AssertValid() const
{CView::AssertValid();
}void CHiView::Dump(CDumpContext& dc) const
{CView::Dump(dc);
}CHiDoc* CHiView::GetDocument() // non-debug version is inline
{ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHiDoc)));return (CHiDoc*)m_pDocument;
}
#endif //_DEBUG/
// CHiView message handlers
// hiView.h : interface of the CHiView class
//
/#if !defined(AFX_HIVIEW_H__2B74F3D9_013A_447B_9B8C_1E81ABDB4769__INCLUDED_)
#define AFX_HIVIEW_H__2B74F3D9_013A_447B_9B8C_1E81ABDB4769__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000class CHiView : public CView
{
protected: // create from serialization onlyCHiView();DECLARE_DYNCREATE(CHiView)// Attributes
public:CHiDoc* GetDocument();// Operations
public:void ZhongZiTC4(int seedx, int seedy, COLORREF fcolor, COLORREF bcolor, CDC* pDC);// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CHiView)public:virtual void OnDraw(CDC* pDC);  // overridden to draw this viewvirtual BOOL PreCreateWindow(CREATESTRUCT& cs);protected:virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);//}}AFX_VIRTUAL// Implementation
public:virtual ~CHiView();
#ifdef _DEBUGvirtual void AssertValid() const;virtual void Dump(CDumpContext& dc) const;
#endifprotected:// Generated message map functions
protected://{{AFX_MSG(CHiView)// NOTE - the ClassWizard will add and remove member functions here.//    DO NOT EDIT what you see in these blocks of generated code !//}}AFX_MSGDECLARE_MESSAGE_MAP()
};#ifndef _DEBUG  // debug version in hiView.cpp
inline CHiDoc* CHiView::GetDocument(){ return (CHiDoc*)m_pDocument; }
#endif///{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_HIVIEW_H__2B74F3D9_013A_447B_9B8C_1E81ABDB4769__INCLUDED_)

直线段裁剪:根据编码裁剪算法的基本原理,编写直线段裁剪的应用程序,并运行演示裁剪效果。

// zhiView.cpp : implementation of the CZhiView class
//#include "stdafx.h"
#include "zhi.h"#include "zhiDoc.h"
#include "zhiView.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/
// CZhiView
#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8void Encode(int x,int y,int *code,int XL,int XR,int YB,int YT){int c=0;if(x<XL)c=c|LEFT;else if(x>XR)c=c|RIGHT;if(y<YB)c=c|BOTTOM;else if(y>YT)c=c|TOP;(*code)=c;}void C_S_Line(POINT &p1,POINT &p2,int XL,int XR,int YB,int YT){int x1,x2,y1,y2,x,y,code1,code2,code;x1=p1.x;x2=p2.x,y1=p1.y;y2=p2.y;Encode(x1,y1,&code1,XL,XR,YB,YT);Encode(x2,y2,&code2,XL,XR,YB,YT);while(code1!=0||code2!=0){if((code1&code2)!=0)return;code=code1;if(code1==0)code=code2;if((LEFT&code)!=0){x=XL;y=y1+(y2-y1)*(XL-x1)/(x2-x1);}else if((code&RIGHT)!=0){x=XR; y=y1+(y2-y1)*(XR-x1)/(x2-x1);}else if((code & BOTTOM)!=0){y=YB; x=x1+(x2-x1)*(YB-y1)/(y2-y1);}else if((TOP & code)!=0){	y=YT; x=x1+(x2-x1)*(YT-y1)/(y2-y1);}if(code==code1){x1=x; y1=y; Encode(x,y,&code1,XL,XR,YB,YT);}else{x2=x; y2=y; Encode(x,y,&code2,XL,XR,YB,YT);}}p1.x=x1; p1.y=y1; p2.x= x2; p2.y=y2;}IMPLEMENT_DYNCREATE(CZhiView, CView)BEGIN_MESSAGE_MAP(CZhiView, CView)//{{AFX_MSG_MAP(CZhiView)// NOTE - the ClassWizard will add and remove mapping macros here.//    DO NOT EDIT what you see in these blocks of generated code!//}}AFX_MSG_MAP// Standard printing commandsON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()/
// CZhiView construction/destructionCZhiView::CZhiView()
{// TODO: add construction code here}CZhiView::~CZhiView()
{
}BOOL CZhiView::PreCreateWindow(CREATESTRUCT& cs)
{// TODO: Modify the Window class or styles here by modifying//  the CREATESTRUCT csreturn CView::PreCreateWindow(cs);
}/
// CZhiView drawingvoid CZhiView::OnDraw(CDC* pDC)
{CZhiDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);// TODO: add draw code for native data hereint sig,XL,XR,YB,YT;float x1,y1,x2,y2;CPoint wMin(150,100),wMax(300,300),p1(100,80),p2(300,330);int color;x1=(float)p1.x;y1=p1.y;x2=p2.x;y2=p2.y;XL=wMin.x;XR=wMax.x;YB=wMin.y;YT=wMax.y;color=RGB(28,28,28);CPen pen1(PS_SOLID,1,color);pDC->SelectObject(&pen1);pDC->Rectangle(XL,YB,XR,YT);color=RGB(255,0,0);CPen pen2(PS_DOT,1,color);pDC->SelectObject(&pen2);pDC->MoveTo(x1,y1);pDC->LineTo(x2,y2);C_S_Line(p1,p2,XL,XR,YB,YT);color=RGB(0,0,255);CPen pen3(PS_SOLID,3,color);pDC->SelectObject(&pen3);pDC->MoveTo(p1.x,p1.y);pDC->LineTo(p2.x,p2.y);}/
// CZhiView printingBOOL CZhiView::OnPreparePrinting(CPrintInfo* pInfo)
{// default preparationreturn DoPreparePrinting(pInfo);
}void CZhiView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{// TODO: add extra initialization before printing
}void CZhiView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{// TODO: add cleanup after printing
}/
// CZhiView diagnostics#ifdef _DEBUG
void CZhiView::AssertValid() const
{CView::AssertValid();
}void CZhiView::Dump(CDumpContext& dc) const
{CView::Dump(dc);
}CZhiDoc* CZhiView::GetDocument() // non-debug version is inline
{ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CZhiDoc)));return (CZhiDoc*)m_pDocument;
}
#endif //_DEBUG/
// CZhiView message handlers

多边形裁剪:根据逐边裁剪算法的基本原理,编写多边形裁剪的应用程序,并运行演示裁剪效果。

// duo1View.cpp : implementation of the CDuo1View class
//#include "stdafx.h"
#include "duo1.h"#include "duo1Doc.h"
#include "duo1View.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/
// CDuo1View
void TestIntersect(int edge, int tyke, POINT p1, POINT p2, POINT &pout, int &yes, int &isIn, CDC* pDC)
{float dx, dy, m;isIn = 0;yes = 0;dy = p2.y - p1.y;dx = p2.x - p1.x;m = dy / dx;switch(tyke){case 1:  /*right*/if(p2.x <= edge){isIn = 1;if(p1.x > edge)yes = 1;}else if(p1.x <= edge)yes = 1;break;case 2:  /*bottom*/if(p2.y >= edge){isIn = 1;if(p1.y < edge)yes = 1;}else if(p1.y >= edge)yes = 1;break;case 3:  /*left*/if(p2.x >= edge){isIn = 1;if(p1.x < edge)yes = 1;}else if(p1.x >= edge)yes = 1;break;case 4:  /*top*/if(p2.y <= edge){isIn = 1;if(p1.y > edge)yes = 1;}else if(p1.y <= edge)yes = 1;break;default: break;}if(yes){if((tyke == 1) || (tyke == 3)){pout.x = edge;pout.y = p1.y + m * (pout.x - p1.x);}if((tyke ==2) || (tyke == 4)){pout.y = edge;pout.x = p1.x + (pout.y - p1.y) / m;}}
}
void clipSingleEdge(int edge, int tyke, int nin, POINT pin[50], int &nout, POINT pout[50], CDC* pDC)
{int i, k = 0, yes, isIn;POINT p, tp, pinters;p.x = pin[nin - 1].x;p.y = pin[nin - 1].y;for(i = 0; i < nin; i ++){TestIntersect(edge, tyke, p, pin[i], pinters, yes, isIn, pDC);if(yes){pout[k].x = pinters.x;pout[k].y = pinters.y;k ++;}if(isIn){pout[k].x = pin[i].x;pout[k].y = pin[i].y;k ++;}p.x = pin[i].x;p.y = pin[i].y;}nout = k;
}
void ClipEdgePolygon(POINT ps[50], int &n, int XL, int XR, int YB, int YT, CDC* pDC)
{int n1 = 0;POINT pt[50];clipSingleEdge(XR, 1, n, ps, n1, pt, pDC);clipSingleEdge(YB, 2, n1, pt, n, ps, pDC);clipSingleEdge(XL, 3, n, ps, n1, pt, pDC);clipSingleEdge(YT, 4, n1, pt, n, ps, pDC);
}IMPLEMENT_DYNCREATE(CDuo1View, CView)BEGIN_MESSAGE_MAP(CDuo1View, CView)//{{AFX_MSG_MAP(CDuo1View)// NOTE - the ClassWizard will add and remove mapping macros here.//    DO NOT EDIT what you see in these blocks of generated code!//}}AFX_MSG_MAP// Standard printing commandsON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()/
// CDuo1View construction/destructionCDuo1View::CDuo1View()
{// TODO: add construction code here}CDuo1View::~CDuo1View()
{
}BOOL CDuo1View::PreCreateWindow(CREATESTRUCT& cs)
{// TODO: Modify the Window class or styles here by modifying//  the CREATESTRUCT csreturn CView::PreCreateWindow(cs);
}/
// CDuo1View drawingvoid CDuo1View::OnDraw(CDC* pDC)
{// 获取文档指针CDuo1Doc* pDoc = static_cast<CDuo1Doc*>(GetDocument());ASSERT_VALID(pDoc);// 绘制代码int i, XL, XR, YB, YT, n = 3, nout = 0;int color;CPoint ply[50] = {CPoint(75, 50), CPoint(160, 120), CPoint(200, 250)};CPoint wMin(100, 100), wMax(200, 200);XL = wMin.x;YB = wMin.y;XR = wMax.x;YT = wMax.y;// 绘制边框color = RGB(28, 28, 28);CPen pen1(PS_SOLID, 1, color);pDC->SelectObject(&pen1);pDC->Rectangle(XL, YB, XR, YT);// 绘制多边形color = RGB(255, 0, 0);CPen pen2(PS_DASHDOT, 1, color);pDC->SelectObject(&pen2);pDC->Polygon(ply, n);// 多边形裁剪ClipEdgePolygon(ply, n, XL, XR, YB, YT, pDC);// 绘制裁剪后的多边形color = RGB(0, 0, 255);CPen pen3(PS_SOLID, 1, color); // 使用蓝色画笔pDC->SelectObject(&pen3);pDC->Polygon(ply, n); // 绘制裁剪后的多边形
}
/
// CDuo1View printingBOOL CDuo1View::OnPreparePrinting(CPrintInfo* pInfo)
{// default preparationreturn DoPreparePrinting(pInfo);
}void CDuo1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{// TODO: add extra initialization before printing
}void CDuo1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{// TODO: add cleanup after printing
}/
// CDuo1View diagnostics#ifdef _DEBUG
void CDuo1View::AssertValid() const
{CView::AssertValid();
}void CDuo1View::Dump(CDumpContext& dc) const
{CView::Dump(dc);
}CDuo1Doc* CDuo1View::GetDocument() // non-debug version is inline
{ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDuo1Doc)));return (CDuo1Doc*)m_pDocument;
}
#endif //_DEBUG/
// CDuo1View message handlers

这篇关于NEFU计算机图形学实验二的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/942476

相关文章

计算机毕业设计 大学志愿填报系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试

🍊作者:计算机编程-吉哥 🍊简介:专业从事JavaWeb程序开发,微信小程序开发,定制化项目、 源码、代码讲解、文档撰写、ppt制作。做自己喜欢的事,生活就是快乐的。 🍊心愿:点赞 👍 收藏 ⭐评论 📝 🍅 文末获取源码联系 👇🏻 精彩专栏推荐订阅 👇🏻 不然下次找不到哟~Java毕业设计项目~热门选题推荐《1000套》 目录 1.技术选型 2.开发工具 3.功能

STM32(十一):ADC数模转换器实验

AD单通道: 1.RCC开启GPIO和ADC时钟。配置ADCCLK分频器。 2.配置GPIO,把GPIO配置成模拟输入的模式。 3.配置多路开关,把左面通道接入到右面规则组列表里。 4.配置ADC转换器, 包括AD转换器和AD数据寄存器。单次转换,连续转换;扫描、非扫描;有几个通道,触发源是什么,数据对齐是左对齐还是右对齐。 5.ADC_CMD 开启ADC。 void RCC_AD

计算机视觉工程师所需的基本技能

一、编程技能 熟练掌握编程语言 Python:在计算机视觉领域广泛应用,有丰富的库如 OpenCV、TensorFlow、PyTorch 等,方便进行算法实现和模型开发。 C++:运行效率高,适用于对性能要求严格的计算机视觉应用。 数据结构与算法 掌握常见的数据结构(如数组、链表、栈、队列、树、图等)和算法(如排序、搜索、动态规划等),能够优化代码性能,提高算法效率。 二、数学基础

HNU-2023电路与电子学-实验3

写在前面: 一、实验目的 1.了解简易模型机的内部结构和工作原理。 2.分析模型机的功能,设计 8 重 3-1 多路复用器。 3.分析模型机的功能,设计 8 重 2-1 多路复用器。 4.分析模型机的工作原理,设计模型机控制信号产生逻辑。 二、实验内容 1.用 VERILOG 语言设计模型机的 8 重 3-1 多路复用器; 2.用 VERILOG 语言设计模型机的 8 重 2-1 多

java计算机毕设课设—停车管理信息系统(附源码、文章、相关截图、部署视频)

这是什么系统? 资源获取方式在最下方 java计算机毕设课设—停车管理信息系统(附源码、文章、相关截图、部署视频) 停车管理信息系统是为了提升停车场的运营效率和管理水平而设计的综合性平台。系统涵盖用户信息管理、车位管理、收费管理、违规车辆处理等多个功能模块,旨在实现对停车场资源的高效配置和实时监控。此外,系统还提供了资讯管理和统计查询功能,帮助管理者及时发布信息并进行数据分析,为停车场的科学

《计算机视觉工程师养成计划》 ·数字图像处理·数字图像处理特征·概述~

1 定义         从哲学角度看:特征是从事物当中抽象出来用于区别其他类别事物的属性集合,图像特征则是从图像中抽取出来用于区别其他类别图像的属性集合。         从获取方式看:图像特征是通过对图像进行测量或借助算法计算得到的一组表达特性集合的向量。 2 认识         有些特征是视觉直观感受到的自然特征,例如亮度、边缘轮廓、纹理、色彩等。         有些特征需要通

【python计算机视觉编程——7.图像搜索】

python计算机视觉编程——7.图像搜索 7.图像搜索7.1 基于内容的图像检索(CBIR)从文本挖掘中获取灵感——矢量空间模型(BOW表示模型)7.2 视觉单词**思想****特征提取**: 创建词汇7.3 图像索引7.3.1 建立数据库7.3.2 添加图像 7.4 在数据库中搜索图像7.4.1 利用索引获取获选图像7.4.2 用一幅图像进行查询7.4.3 确定对比基准并绘制结果 7.

【python计算机视觉编程——8.图像内容分类】

python计算机视觉编程——8.图像内容分类 8.图像内容分类8.1 K邻近分类法(KNN)8.1.1 一个简单的二维示例8.1.2 用稠密SIFT作为图像特征8.1.3 图像分类:手势识别 8.2贝叶斯分类器用PCA降维 8.3 支持向量机8.3.2 再论手势识别 8.4 光学字符识别8.4.2 选取特征8.4.3 多类支持向量机8.4.4 提取单元格并识别字符8.4.5 图像校正

Python计算机视觉编程 第十章

目录 一、OpenCv基础知识 1.读取和写入图像 2.颜色空间 3.显示图像和结果 二、处理视频 1.输入视频 2.将视频读取到NumPy数组中 三、跟踪 1.光流 2.Lucas-Kanade算法 一、OpenCv基础知识 OpenCV 自带读取、写入图像函数以及矩阵操作和数学库。 1.读取和写入图像 import cv2# 读取图像im = c

2025届计算机毕业设计:如何构建Java SpringBoot+Vue个人健康档案管理系统?

✍✍计算机编程指导师 ⭐⭐个人介绍:自己非常喜欢研究技术问题!专业做Java、Python、微信小程序、安卓、大数据、爬虫、Golang、大屏等实战项目。 ⛽⛽实战项目:有源码或者技术上的问题欢迎在评论区一起讨论交流! ⚡⚡ Java实战 | SpringBoot/SSM Python实战项目 | Django 微信小程序/安卓实战项目 大数据实战项目 ⚡⚡文末获取源码 文章目录