利用C语言编写 能够画出任意斜率的直线算法程序(利用中点画线法...
发布网友
发布时间:2024-02-15 20:15
我来回答
共1个回答
热心网友
时间:2024-07-19 01:30
将DDA算法改成中点划线算法即可
// DDA画线View.cpp : implementation of the CDDAView class
//
#include "stdafx.h"
#include "DDA画线.h"
#include "DDA画线Doc.h"
#include "DDA画线View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDDAView
IMPLEMENT_DYNCREATE(CDDAView, CView)
BEGIN_MESSAGE_MAP(CDDAView, CView)
//{{AFX_MSG_MAP(CDDAView)
// 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 commands
ON_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()
/////////////////////////////////////////////////////////////////////////////
// CDDAView construction/destruction
CDDAView::CDDAView()
{
// TODO: add construction code here
}
CDDAView::~CDDAView()
{
}
BOOL CDDAView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CDDAView drawing
void CDDAView::OnDraw(CDC* pDC)
{
CDDADoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
int xx,yy,x2,y2,m,n;
float dx,dy,k,x,y;
int x0=50,y0=500,x1=50,y1=50;
if(x0>x1)
{
m=x0;x0=x1;x1=m;
m=y0;y0=y1;y1=m;
}
dx=x1-x0;
dy=y1-y0;
k=dy/dx;
if(x0==x1)
{
if(y0>y1)
{
n=y0;
y0=y1;
y1=n;
}
for(y2=y0;y2<=y1;)
{
for(n=-10;n<11;)
{
pDC->SetPixel(x0+n,y2,255);
n++;
}
y2=y2+3;
}
}
if(k>=-1&&k<=1.0)
{
y=y0;
for(x2=x0;x2<=x1;)
{
yy=(int)(y+0.5);
for(n=-10;n<11;)
{
pDC->SetPixel(x2,yy+n,255);
n++;
}
y=y+k;
x2++;
}
}
else if(k>1)
{
x=x0;
k=dx/dy;
for(y2=y0;y2<=y1;)
{
xx=(int)(x+0.5);
for(n=-10;n<11;)
{
pDC->SetPixel(xx+n,y2,255);
n++;
}
x=x+k;
y2++;
}
}
else if(k<-1)
{
x=x1;
k=dx/dy;
for(y2=y1;y2<=y0;)
{
xx=(int)(x+0.5);
for(n=-10;n<11;)
{
pDC->SetPixel(xx+n,y2,255);
n++;
}
x=x+k;
y2++;
}
}
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CDDAView printing
BOOL CDDAView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDDAView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDDAView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CDDAView diagnostics
#ifdef _DEBUG
void CDDAView::AssertValid() const
{
CView::AssertValid();
}
void CDDAView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CDDADoc* CDDAView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDDADoc)));
return (CDDADoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDDAView message handlers