Draw a straight line with any slope in computer graphics (1)

Source: Internet
Author: User

Qing Dujun

Address: http://blog.csdn.net/qingdujun/article/details/40025917


This article demonstrates how to write a line function (filled with pixels) by yourself to draw a line of any slope.

1) Create CP2 class

Header file: p2.h

// P2.h: interface for the CP2 class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_P2_H__85E77AD6_0704_4E12_8E74_5BE1744B3154__INCLUDED_)#define AFX_P2_H__85E77AD6_0704_4E12_8E74_5BE1744B3154__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000//二维点类class CP2  {public:CP2();CP2(double x,double y);virtual ~CP2();public:         //方便访问,直接定义为共有double x;double y;};#endif // !defined(AFX_P2_H__85E77AD6_0704_4E12_8E74_5BE1744B3154__INCLUDED_)
Implementation file: p2.cpp
// P2.cpp: implementation of the CP2 class.////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "DrawLine.h"#include "P2.h"#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE[]=__FILE__;#define new DEBUG_NEW#endif//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////CP2::CP2(){x=0.0;y=0.0;}CP2::CP2(double x,double y){this->x=x;this->y=y;}CP2::~CP2(){}
2) create a cline class

Header file: line. h

// Line.h: interface for the CLine class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_LINE_H__8A91E283_F40D_4086_8BB8_7A9D9A6DB4D5__INCLUDED_)#define AFX_LINE_H__8A91E283_F40D_4086_8BB8_7A9D9A6DB4D5__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000#include "P2.h"class CLine  {public:CLine();virtual ~CLine();void MoveTo(CP2 p0);          //移动到指定位置void MoveTo(double x, double y);void LineTo(CP2 p1, CDC *pDC);    //绘制直线,不含终点void LineTo(double x, double y, CDC *pDC);private:CP2 P0;      //起点CP2 P1;      //终点};#endif // !defined(AFX_LINE_H__8A91E283_F40D_4086_8BB8_7A9D9A6DB4D5__INCLUDED_)
Implementation file: line. cpp
// Line.cpp: implementation of the CLine class.////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "DrawLine.h"#include "Line.h"#include "math.h"#define Round(d) int(floor(d+0.5))//四舍五入宏定义#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE[]=__FILE__;#define new DEBUG_NEW#endif//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////CLine::CLine(){}CLine::~CLine(){}void CLine::MoveTo(CP2 p0) //记录直线起点函数{P0=p0;}void CLine::MoveTo(double x, double y){P0.x = x;P0.y = y;}void CLine::LineTo(double x, double y, CDC *pDC) //绘制{CP2 p;p.x = x;p.y = y;LineTo(p, pDC);}void CLine::LineTo(CP2 p1, CDC *pDC){P1=p1;CP2 p,t;COLORREF clr = RGB(0,0,0);  //黑色像素点if(fabs(P0.x-P1.x)<1e-6)    //绘制垂线{if(P0.y>P1.y)           //交换顶点,使得起始点低于终点{t=P0;P0=P1;P1=t;}for(p=P0;p.y<P1.y;p.y++)  //执行绘制,填充像素点{pDC->SetPixelV(Round(p.x),Round(p.y),clr);}}else{double k,d;k=(P1.y-P0.y)/(P1.x-P0.x); //斜率if(k>1.0) //绘制k>1(y为主方向){if(P0.y>P1.y){t=P0;P0=P1;P1=t;}d=1-0.5*k;    //中点初始值for(p=P0;p.y<P1.y;p.y++){pDC->SetPixelV(Round(p.x),Round(p.y), clr);                if(d>=0)  //中点位于将绘制点上方,填充下方点{p.x++;d=d+1-k;    //递推公式:当d>=0时,d(i+1)=d(i)+1-k;}else                     d+=1;       }}if(0.0<=k && k<=1.0) //绘制0<=k<=1(x为主方向){if(P0.x>P1.x){t=P0;P0=P1;P1=t;}d=0.5-k;     //中点初始值for(p=P0;p.x<P1.x;p.x++){pDC->SetPixelV(Round(p.x),Round(p.y), clr);                if(d<0)  //中点位于将绘制点下方,填充上方点{p.y++;d=d+1-k; //递推公式:当d<0时,d(i+1)=d(i)+1-k;}else d-=k;}}if(k>=-1.0 && k<0.0)//绘制-1<=k<0(x为主方向){if(P0.x>P1.x){t=P0;P0=P1;P1=t;}d=-0.5-k;        //中点初始化            for(p=P0;p.x<P1.x;p.x++){pDC->SetPixelV(Round(p.x),Round(p.y), clr);                if(d>0)      //中点位于将绘制点上方,填充下方点{p.y--;d=d-1-k; //递推公式:当d>0时,d(i+1)=d(i)-1-k;}else d-=k;}}if(k<-1.0)//绘制k<-1 (y为主方向){if(P0.y<P1.y){t=P0;P0=P1;P1=t;}d=-1-0.5*k; //中点初始化for(p=P0;p.y>P1.y;p.y--){pDC->SetPixelV(Round(p.x),Round(p.y), clr);                if(d<0) //中点位于将绘制点下方,填充上方点{p.x++;d=d-1-k;//递推公式:当d>0时,d(i+1)=d(i)-1-k;}else d-=1;           }}}P0=p1;  //将终点赋值给起点}
3) ondraw Function

void CDrawLineView::OnDraw(CDC* pDC){CDrawLineDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);// TODO: add draw code for native data here//绘制直线CLine *line=new CLine;//动态创建直线绘制类对象line->MoveTo(100,100);line->LineTo(500,500, pDC);}
4) The rendering effect is as follows:



Address: http://blog.csdn.net/qingdujun/article/details/40025917

References: tutorial on basic computer graphics (Visual C ++) (2nd) by Kong lingde


draw a straight line with any slope in computer graphics (1)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.