导航:首页 > 源码编译 > ellipse函数源码

ellipse函数源码

发布时间:2022-04-22 10:55:12

㈠ MFC编程ellipse()怎么画圆

所谓的动态画线,画矩形,画椭圆的本质就是在于消隐问题的解决以及对Windows消息循环机制的理解,消隐问题:因为MFC中有一个函数SetROP2,通过将该函数的参数设置为R2—NOT(当前绘制的像素值设为屏幕像素值的反色,这里面的“屏幕”二字是指你所绘制的图形所占据的那一部分屏幕区域,即直线所占的屏幕区域就是直线所在的那一段线的区域),利用这一点通过在同一区域重复画两次便可以将该区域的形状消隐。

而Windows消息循环机制更好的体现于MouseMove消息响应中,因为要实现动态画线、画矩形这就体现在鼠标在绘图区的移动上,鼠标在绘图区移动时,产生动态的效果,也就是说在mouse 移动的过程中始终在画线、画矩形,这就要求在MouseMove消息响应中有画线、画矩形的相应函数的调用,但正是因为mouse移动的过程中画了很多的线、矩形,所以我们就要在mouse移动的过程中将之前画出的线、矩形消隐掉,才能保证实现了自己所要实现的功能——动态画线、画矩形,但又没有产生多余的线和矩形。
消隐的问题和动态画图的问题关键在于MouseMove消息响应中的函数调用的顺序:
1.首先将上次所画的图形消隐掉
2.画出临时的图形
3.消息循环的机制:循环执行上述代码(这是Windows循环的机制,不需要自己代码实现)

在最后的LButtonUp消息响应中,需要将上面的MouseMove消息响应中循环的最后一次中最后所画的临时图形消隐掉,之后根据LButtonUp消息响应中point参数画出最终的图形。综上可知:MouseMove中的代码和LButtonUp中消隐临时图形的代码所要实现的功能就是动态这一过程,而图形的最终绘制还在与LButtonUp消息响应及其point参数。
代码如下:
[cpp] view plain
private:
[cpp] view plain
//......
HCURSOR m_HCross;
UINT m_drawType;
CPoint m_OldPoint;
CPoint m_startPoint;
BOOL m_startRect;
[cpp] view plain
CGISView::CGISView()
{
// TODO: add construction code here
//....
m_startRect=FALSE;
m_startPoint=0;
m_OldPoint=0;
m_drawType=0;
//初始化m_HCross为十字光标
m_HCross=AfxGetApp()->LoadStandardCursor(IDC_CROSS);

}
[cpp] view plain
void CGISView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_startRect=TRUE; //鼠标左键单击,设置可以开始绘制矩形框
m_startPoint=point; //记录起始点
m_OldPoint=point;//设置老点也为起始点
//设置光标为十字光标
::SetCursor(m_HCross);

CView::OnLButtonDown(nFlags, point);
}
[cpp] view plain
void CGISView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default

CClientDC dc(this); //获取设备句柄
dc.SetROP2(R2_NOT);//此为关键
dc.SetROP2(R2_NOT);//所绘制的图形并没有消失,所以可以验证下面的连续两次颜色取反不是在一次相应中完成
//SetRop2 Specifies the new drawing mode.(MSDN)
//R2_NOT Pixel is the inverse of the screen color.(MSDN)
//即:该函数用来定义绘制的颜色,而该参数则将颜色设置为原屏幕颜色的反色
//这样,如果连续绘制两次的话,就可以恢复原来屏幕的颜色了(如下)
//但是,这里的连续两次绘制却不是在一次消息响应中完成的
//而是在第一次拖动响应的绘制可以显示(也就是看到的),第二次拖动绘制实现擦出(也就看不到了)

dc.SelectStockObject(NULL_BRUSH);//不使用画刷
if(TRUE==m_startRect) //根据是否有单击判断是否可以画矩形
{
switch(m_drawType)
{
case 1://Rectangle

::SetCursor(m_HCross);
dc.Rectangle(CRect(m_startPoint,m_OldPoint));
dc.Rectangle(CRect(m_startPoint,point));
m_OldPoint=point;

break;

case 2: //Line
::SetCursor(m_HCross);
//擦去上一次绘制的临时线
dc.MoveTo(m_startPoint);
dc.LineTo(m_OldPoint);

//绘制这一次的临时线
dc.MoveTo(m_startPoint);
dc.LineTo(point);

//将临时线的终点复制给m_OldPoint,
//使其在消息循环的过程中将该值传递到
//擦去上一次画线的过程中,以便擦去上一次所画的线
m_OldPoint=point;

break;
case 3: //Circle
::SetCursor(m_HCross);
//擦去上一次绘制的临时圆
//设定该圆的y坐标,因为要保证两点的x之差等于y之差
m_OldPoint.y=m_OldPoint.x-m_startPoint.x+m_startPoint.y;
dc.Ellipse(CRect(m_startPoint,m_OldPoint));

//绘制临时圆

point.y=point.x-m_startPoint.x+m_startPoint.y;
dc.Ellipse(CRect(m_startPoint,point));

m_OldPoint=point;
break;
case 4: //Ellipse
::SetCursor(m_HCross);

dc.Ellipse(CRect(m_startPoint,m_OldPoint));
dc.Ellipse(CRect(m_startPoint,point));
m_OldPoint=point;
break;
case 5: //Dot

break;

}
[cpp] view plain
}
[cpp] view plain
}
[cpp] view plain
void CGISView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_startRect=FALSE;//重置绘制矩形框标志
::ClipCursor(NULL);//解锁光标,即将光标恢复为默认光标
//消隐最后的一个矩形
CClientDC dc(this);//获取设备句柄
// dc.SetROP2(R2_NOT); //在MouseMove消息响应中使用过该函数了,所以在这里再一次使用会使得其恢复为屏幕的颜色
dc.SelectStockObject(NULL_BRUSH); //设置画刷为空画刷
switch(m_drawType)
{
case 1: //Retangle

//利用当前画刷绘制矩形,内部由当前空画刷(NULL_BRUSH)填充。
//擦去MouseMove消息响应中的临时矩形
dc.Rectangle(CRect(m_startPoint,m_OldPoint));
//绘制固定矩形
dc.Rectangle(CRect(m_startPoint,point));

//m_drawType=0; //该行的可以使得每画一次都要选择所画的形状
break;

case 2: //Line

//擦去MouseMove消息响应中绘制的最后一次临时线
dc.MoveTo(m_startPoint);
dc.LineTo(m_OldPoint);

//绘制固定线
dc.MoveTo(m_startPoint);
dc.LineTo(point);
break;
case 3: //Circle

dc.Ellipse(CRect(m_startPoint,m_OldPoint));
dc.Ellipse(CRect(m_startPoint,point));
break;
case 4: //Ellipse

dc.Ellipse(CRect(m_startPoint,m_OldPoint));
dc.Ellipse(CRect(m_startPoint,point));
break;
case 5: //Dot
dc.SetPixel(point,RGB(0,0,0));
break;

}
CView::OnLButtonUp(nFlags, point);
}
[cpp] view plain
void CGISView::OnRectangle()
{
// TODO: Add your command handler code here
m_drawType=1;//设置所画的为矩形

}

void CGISView::OnLine()
{
// TODO: Add your command handler code here
m_drawType=2;
}

void CGISView::OnEllipse()
{
// TODO: Add your command handler code here
m_drawType=4;
}

void CGISView::OnDot()
{
// TODO: Add your command handler code here
m_drawType=5;

}

void CGISView::OnCircle()
{
// TODO: Add your command handler code here
m_drawType=3;
}

以上为自己根据实际编程练习和对博文的学习所领悟到的,如果有哪里理解的不对,还望大家指正,谢谢哈!(代码没有问题,可以正常运行。)
注:代码的排版,我多次修改,并确保整齐,但是一旦我发表后,代码的排版就会发生改变,有几行代码就会变得不规则。

㈡ c#椭圆代码

如何基于C#实现椭圆绘制

马燕鹏 王文新

通常所用的光栅显示器可看成是一个像素点阵的集合,绘图软件绘制椭圆就是在此集合中寻找最逼近椭圆弧的点并按给定颜色显示的过程。输入参数一般为椭圆的外接矩形而非椭圆方程,但可通过该矩形算出它的长、短半轴和椭圆中心,即得到了所绘椭圆的方程。
由于标准椭圆(中心在原点的椭圆)具有对称性,找到第一象限的逼近点根据对称关系就可得到其他象限的逼近点,所以绘制椭圆时先寻找对应标准椭圆的逼近点,然后平移回椭圆的中心,从而能够简化计算提高效率。
标准椭圆的方程为 ,即 。对其求导得,可见椭圆弧的斜率从0到。以斜率 即 为界把椭圆弧分成两部分,第一部分可得 , 的变化量小于的变化量,应以递增步长来确定,第二部分可得,的变化量小于的变化量,应以递增步长来确定。
对于第一部分,假设已知点为则下一点在其右方点或右下方点中选择离椭圆弧最近的点,而为右方点到椭圆弧的距离,为右下方点到椭圆弧的距离,通过做差:

可判断出所要选择的点,即:选择右方的点,选择右下方的点。根据所得到的利用上述思想推下一点即可得到与的递推关系式:

根据递推关系式和已知点利用循环就可得到第一部分所有的点,循环的条件为即。
对于第二部分,假设已知点为则下一点在其上方 点或左上方点中选择离椭圆弧最近的点,而为上方点到椭圆弧的距离,为左上方点到椭圆弧的距离,通过做差:

可判断出所要选择的点,即:选择上方的点,选择左上方的点。根据所得到的利用上述思想推下一点即可得到 与的递推关系式:

根据递推关系式和已知点利用循环就可得到第二部分所有的点,循环的条件为即。
通过分析可把绘制椭圆的功能封装到一个类(如TDrawEllipse)中,利用构造函数初始化要绘制椭圆的位图,利用公有方法Draw来绘制椭圆,该方法首先计算椭圆的长、短半轴和椭圆中心,然后调用私有方法PlotPartI和PlotPartII来绘制椭圆,利用只读属性Image得到绘制后的位图,实现的核心如下:
public class TDrawEllipse
{
private Bitmap image;//存储生成椭圆的位图
public Bitmap Image
{
get
{ return this.image; //返回绘制好椭圆的位图 }
}
private int oX, oY, a, b;//存储椭圆的中心和长短半轴
private Color color;//存储椭圆的颜色
private void PlotPoints(int x, int y) //绘制坐标点
{
//由于算法是以(0,0)点为椭圆中心构造的,绘制椭圆时需要把传递的坐标
//平移到真正的椭圆中心(oX,oY)并绘制其它象限的像素点。
this.image.SetPixel(this.oX + x, this.oY + y, this.color);
this.image.SetPixel(this.oX - x, this.oY + y, this.color);
this.image.SetPixel(this.oX - x, this.oY - y, this.color);
this.image.SetPixel(this.oX + x, this.oY - y, this.color);
}
private void PlotPartI()//绘制第一部分
{
int x = 0, y = b;
int d = 2 * b * b - 2 * a * a * b + a * a;
while (b * b * x < a * a * y)
{
PlotPoints(x, y);
if (d < 0)
{ x = x + 1; d = d + 4 * b * b * x + 2 * b * b; }
else
{ x = x + 1; y = y - 1;
d = d + 4 * b * b * x + 2 * b * b - 4 * a * a * y; }
}
}
private void PlotPartII()//绘制第二部分
{
int y = 0, x = a;
int d = 2 * a * a - 2 * a * b * b + b * b;
while (b * b * x >= a * a * y)
{
PlotPoints(x, y);
if (d < 0)
{ y = y + 1;d = d + 4 * a * a * y + 2 * a * a; }
else
{ y = y + 1;x = x - 1;
d = d + 4 * a * a * y + 2 * a * a - 4 * b * b * x; }
}
}
public TDrawEllipse(Bitmap image)//构造函数,初始化绘制椭圆的位图
{
this.image = image;
}
public void Draw(Rectangle rt, Color color)//绘制椭圆的函数
{
//参数 Rectangle rt是要绘制椭圆的外接矩形
//参数 Color color是要绘制椭圆的颜色
this.oX = rt.Left + rt.Width / 2;//计算椭圆的中心坐标
this.oY = rt.Top + rt.Height / 2;
this.a = rt.Width / 2;//计算椭圆的长短半轴
this.b = rt.Height / 2;
this.color = color;
PlotPartI();//绘制第一部分
PlotPartII();//绘制第二部分
}
}
有了封装绘制椭圆功能的类之后,就可以在程序中进行实例化得到该类的一个对象,利用该对象调用Draw方法就可绘制出椭圆来,从而完成绘图系统中绘制椭圆功能的底层实现。以上代码在VS.NET2005中编译通过运行正常。

㈢ ellipse(250,250,0,360,top,bottom); 有哪位大神能帮我分析下,ellipse()函数里面的六个数字分别代表的含

ellipse 函数并不是 c 语言标准库里面的,所以,不同的公司对这个函数的规定不一样。

borland 公司对这个函数的规定如下:
ellipse(圆心x,圆心y,起始角,终止角,x半径,y半径);

微软对这个函数的规定如下:
Ellipse(DC句柄,left,top,right,bottom); // 注:left, top, right, bottom 是椭圆外切矩形的坐标

EasyX 库对这个函数的规定如下:
ellipse(left, top, right, bottom); // 注:left, top, right, bottom 是椭圆外切矩形的坐标

初学 vc6 绘图常用的库一般是 easyx,所以你可以使用 4 个参数试试。如果和你想要的结果不一样,简单的做一下参数转换就行。比如,将你的代码由 borland 公司的参数用在 easyx 上可以这么写:
ellipse(250 - top, 250 - bottom, 250 + top, 250 + botom);

㈣ vc++如何用pDC->Ellipse()画椭圆的一部分

圆弧不要用Ellipse画,用Arc函数画

pDC->Arc(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4);

由(x1, y1, x2, y2)确定椭圆外接矩形,(x3, y3, x4, y4)确定圆弧起点与终点(不一定在所要圆弧上,但必须使其与圆心的连线与前确定的椭圆有交点)

㈤ C中画椭圆的工具一般最多五个参数,而下面的函数中却有六个,请帮忘解释下每个参数的含义:ellipse(250,25

ellipse(250,250,0,360,top,bottom);
参数从左往右分别是:
圆心x坐标,圆心y坐标,起始角度,终止角度,x轴半径,y轴半径

㈥ VC中画线,moveto,lineto;画圆:.ellipse,具体说明函数原理.

MoveTo( int x, int y );
Moves the current position to the point specified by x and y
(or by point).
画线段的起点
LineTo( int x, int y );
Draws a line from the current position up to, but not including, the point
specified by x and y (or point). The line is drawn with the
selected pen. The current position is set to x,y or to
point.
画线段的终点

BOOL Ellipse( int x1, int y1,
int x2, int y2 );
Draws an ellipse. The center of the ellipse is the center of the bounding
rectangle specified by x1, y1, x2, and y2, or
lpRect. The ellipse is drawn with the current pen, and its interior is
filled with the current brush.
根据两个点画椭圆

㈦ MFC中 ellipse 函数代码在哪里查看

看不到,MFC不是开源的.

㈧ Ellipse函数的4个参数分别是什么意思

Declare Function Ellipse Lib "gdi32" Alias "Ellipse" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
描绘一个椭圆,由指定的矩形围绕。椭圆用当前选择的画笔描绘,并用当前选择的刷子填充

hdc Long,设备场景的句柄
X1,Y1 Long,约束矩形采用逻辑坐标的左上角位置
X2,Y2 Long,约束矩形采用逻辑坐标的右下角位置

㈨ 编java代码求椭圆和长方形的面积和周长。

没明白isLargeThan是什么意思,能说得详细点儿么?
先把满足前四个条件的程序发给你,你看看行不行。
注:一个类一个java文件,运行Test3类执行。

public class Point {
private double x;
private double y;
public Point() {
x=0;
y=0;
}
public Point(double x,double y){
this.x=x;
this.y=y;
}
public double getX(){
return this.x;
}
public double getY(){
return this.y;
}
public void setX(double x){
this.x=x;
}
public void setY(double y){
this.y=y;
}
public Point translate(double u,double v){
this.x=this.x+u;
this.y=this.y+v;
return new Point (this.x,this.y);
}
}
public class Rectangle extends Point {
private double height;
private double wideth;
public Rectangle() {
super();
}
public Rectangle(Point p,double h,double w){
super(p.getX(),p.getY());
this.height=h;
this.wideth=w;
}
public double getPerimeter(){
return 2*(height+wideth);
}
public double getArea(){
return height*wideth;
}
}
public class Ellipse extends Point{

private double height;
private double wideth;
public Ellipse() {
super();
}
public Ellipse(Point p,double h,double w){
super(p.getX(),p.getY());
this.height=h;
this.wideth=w;
}
public double getPerimeter(){
return 2*3.14*Math.sqrt((height*height+wideth*wideth)/2);

}
public double getArea(){
return 3.14*height*wideth;
}
}
public class Test3 {

public static void main(String[] args) {
Point p=new Point(1.2,4.6);
Rectangle r=new Rectangle(p,9.2,8.7);
Ellipse e=new Ellipse(p,3.2,9.2);
Point p1=p.translate(2.8,2.9);

System.out.println("移动后的点为x="+p1.getX()+" y="+p1.getY());

System.out.println("长方形的周长为:"+r.getPerimeter());
System.out.println("长方形的面积为:"+r.getArea());
System.out.println("椭圆形的周长为:"+e.getPerimeter());
System.out.println("椭圆形的面积为:"+e.getArea());
}

}

㈩ Ellipse()这个函数是用来干吗的

函数功能:该函数用于画一个椭圆,椭圆的中心是限定矩形的中心,使用当前画笔画椭圆,用当前的画刷填充椭圆。

函数原型:BOOL Ellipse(HDC hdc, int nLeftRect, int nTopRect, nRightRect, int nBottomRect);

参数:

hdc:设备环境句柄。

nLeftRect:指定限定矩形左上角的X坐标。

nTopRect:指定限定矩形左上角的Y坐标。

nRightRect:指定限定矩形右下角的X坐标。

nBottomRect:指定限定矩形右下角的Y坐标。

返回值:如果函数调用成功,返回值非零;如果函数调用失败,返回值是0。

Windows NT:若想获得更多错误信息,请调用GetLastError函数。

备注:椭圆既不使用也不改变当前位置。

Windows 95和Windows 98:限定矩形的坐标值之和不能超过32767。nLeftRect与nRigthRect或nTopRectn与BottomRect之和不能超过32767。

速查:Windows NT:3.1及以上版本;Windows:95及以上版本;Windows CE:1.0及以上版本;头文件:wingdi.h;库文件:gdi32.lib。

阅读全文

与ellipse函数源码相关的资料

热点内容
手机摄像文件夹名 浏览:132
口才训练手册编译口才精品书系 浏览:998
linuxfunc 浏览:269
高德地图解压后的文件 浏览:639
php加水印类 浏览:228
编译原理定义表格和编写查找函数 浏览:350
指数函数和对数函数的高精度快速算法 浏览:209
c预编译干什么 浏览:25
hp网络共享文件夹 浏览:366
程序员如何不被废 浏览:807
二进制流转pdf 浏览:917
php判断爬虫 浏览:572
960除24除4简便算法 浏览:788
关于解压英语翻译 浏览:569
python控制键盘右键 浏览:922
php没有libmysqldll 浏览:830
时政新闻app哪个好 浏览:907
手机已加密怎么办 浏览:202
安卓手机截屏怎么传到苹果 浏览:530
京管家app哪里下载 浏览:34