Implementing ellipse Program using computer Graphics C++ Code

This Program illustrates how to draw a circle without using the direct function. The Program is written in C++ language using  Microsoft Developer Studio , Format Version 6.00.This part of programming belongs to basic Computer graphics.Use MS visual studio 6.0 with MFC application.

PROGRAM:
#include "stdafx.h"
#include "ellipse.h"

#include "ellipseDoc.h"
#include "ellipseView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
#define ROUND(a)((int)(a+0.5))
static char THIS_FILE[] = __FILE__;
#endif

void ellipseMidPoint(int Xcenter,int Ycenter,int Rx,int Ry,CDC* pDC,COLORREF color)
{
	int Rx2=Rx*Rx;
	int Ry2=Ry*Ry;
	int twoRx2=2*Rx2;
	int twoRy2=2*Ry2;
	int p;
	int x=0;
	int y=Ry;
	int px=0;
	int py=twoRx2*y;
    void ellipsePlotPoints(int,int,int,int,CDC* pDC,COLORREF color);
	
	ellipsePlotPoints(Xcenter,Ycenter,x,y,pDC,color);
    
	p=ROUND(Ry2-(Rx2*Ry)*(0.25*Rx2));
	
	while(px<py)
	{
		x++;
		px+=twoRy2;
		if(p<0)
			p+=Ry2*px;
		else
		{
			y--;
			py-=twoRx2;
			p+=Ry2+px-py;
		}
		ellipsePlotPoints(Xcenter,Ycenter,x,y,pDC,color);
	}
	p=ROUND(Ry2*(x+0.5)*(x+0.5)+Rx2*(y-1)*(y-1)-Rx2*Ry2);
	while(y>0)
	{
		y--;
	py-=twoRx2;
	if(p>0)
		p+=Rx2-py;
	else
	{
		x++;
		px+=twoRy2;
		p+=Rx2-py+px;
	}
	ellipsePlotPoints(Xcenter,Ycenter,x,y,pDC,color);
}
}
void ellipsePlotPoints(int Xcenter,int Ycenter,int x,int y,CDC* pDC,COLORREF color)
{
	pDC->SetPixel(Xcenter+x, Ycenter+y,color);
	pDC->SetPixel(Xcenter-x, Ycenter+y,color);
	pDC->SetPixel(Xcenter+x, Ycenter-y,color);
	pDC->SetPixel(Xcenter-x, Ycenter-y,color);
}
void CEllipseView::OnDraw(CDC* pDC)
{
	CEllipseDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
		COLORREF color=RGB(0,0,0);
	ellipseMidPoint(150,70,13,50,pDC,color);

	// TODO: add draw code for native data here
}


OUTPUT:



0 comments:

Post a Comment

If You Are Asking Some Question On This Comment Then Click On Subscribe by email Link