c++ 编写line函数 求解
#include <iostream>
using namespace std;
//宏定义:在大小为RECT_SIZE的源码矩形区域内画图,可修改
#define RECT_SIZE
//定义点类Point
class Point
{
private:
int x; //横坐标
int y; //纵坐标
public:
//构造函数
Point();
//构造函数
Point(int xx, int yy)
{
x = xx;
y = yy;
}
//申明友元函数drawLine,可访问私有成员x,树代y
friend void drawLine(Point a, Point b);
};
//画线函数,从点a到点b画线
void drawLine(Point a,androiddump源码 Point b)
{
int x1, y1, x2, y2;
//取a,b点的坐标
x1 = a.x;
y1 = a.y;
x2 = b.x;
y2 = b.y;
int i, j;
//开始画线:如果某一点在直线ab上,且不在线段ab的延长线或反向延长线上
//则改点为所画线段上的一点,用“*”表示,否则用“ ”表示
for (i = 0; i < RECT_SIZE; i ++)
{
cout << endl;
for (j = 0; j < RECT_SIZE; j ++)
{
if ( (( i-x1)*( i-x1) + ( j-y1)*( j-y1) <=
(x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))&&
(abs((i-x1)*(y2-y1) - (j-y1)*(x2-x1)) <= 4))
{
cout << '*';
}
else
{
cout << ' ';
}
}
}
}
//主函数
int main()
{
//定义两点a、dctracker 源码b,坐标可随意
Point a(2, 4), b(, );
//从a画线到b
drawLine(a, b);
return 0;
}
计算机图形学:线段剪裁中点分割算法,要求用C++做,急求源代码啊,谢谢!
#include <GL/glut.h>#include <stdlib.h>#include "iostream.h"int x0,y0,x1,y1;int Max(int a,int b,int c){ if(a>b) { if(a>c) return a; else return c; } else { if(b>c) return b; else return c; }}int Min(int a,int b,int c){ if(a<b) { if(a<c) return a; else return c; } else { if(b<c) return b; else return c; }}void DrawLine1(int x0,int y0,int x1,int y1){ int d,temp; temp=y0; d=2*(y1-y0)-(x1-x0); glBegin(GL_POINTS); glVertex2d(x0,y0); glEnd(); for(int k=x0+1;k<x1;k++) { if(d>=0) { glBegin(GL_POINTS); glVertex2d(k,temp+1); glEnd(); d=d+2*(y1-y0)-2*(x1-x0); temp=temp+1; } else { glBegin(GL_POINTS); glVertex2d(k,temp); glEnd(); d=d+2*(y1-y0); temp=temp; } } glBegin(GL_POINTS); glVertex2d(x1,y1); glEnd();}void DrawLine2(int x0,int y0,int x1,int y1){ int d,temp; temp=x0; d=2*(x1-x0)-(y1-y0); glBegin(GL_POINTS); glVertex2d(x0,y0); glEnd(); for(int k=y0+1;k<y1;k++) { if(d>=0) { glBegin(GL_POINTS); glVertex2d(temp+1,k); glEnd(); d=d+2*(x1-x0)-2*(y1-y0); temp=temp+1; } else { glBegin(GL_POINTS); glVertex2d(temp,k); glEnd(); d=d+2*(x1-x0); temp=temp; } } glBegin(GL_POINTS); glVertex2d(x1,y1); glEnd();}void DrawTriangle(int x0,int y0,int x1,int y1,int x2,int y2){ int xmin,xmax,ymin,ymax; float a,b,c; xmin=Min(x0,x1,x2); xmax=Max(x0,x1,x2); ymin=Min(y0,y1,y2); ymax=Max(y0,y1,y2); glColor3f(1.0f,0.0f,0.0f); glBegin(GL_POINTS); glVertex2d(x0,y0); glEnd(); glColor3f(0.0f,1.0f,0.0f); glBegin(GL_POINTS); glVertex2d(x1,y1); glEnd(); glColor3f(0.0f,0.0f,1.0f); glBegin(GL_POINTS); glVertex2d(x2,y2); glEnd(); for(float n=ymin;n<=ymax;n++) for(float m=xmin;m<xmax;m++) { a=((y1-y2)*m+(x2-x1)*n+x1*y2-x2*y1)/((y1-y2)*x0+(x2-x1)*y0+x1*y2-x2*y1); b=((y2-y0)*m+(x0-x2)*n+x2*y0-x0*y2)/((y2-y0)*x1+(x0-x2)*y1+x2*y0-x0*y2); c=((y0-y1)*m+(x1-x0)*n+x0*y1-x1*y0)/((y0-y1)*x2+(x1-x0)*y2+x0*y1-x1*y0); if(a>0 && b>0 && c>0) { float color0=a*1.0; float color1=b*1.0; float color2=c*1.0; glColor3f(color0,color1,color2); glBegin(GL_POINTS); glVertex2d(m,n); glEnd(); } } }void display(){ /* clear all pixels */ glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glBegin(GL_POINTS); glVertex2d(x,y); 中间是点的坐标 glEnd(); */ /*下面的语句是画一个白色的正方形*/ if((y1-y0)/(x1-x0)<=1) { DrawLine1(x0,y0,x1,y1); } else { DrawLine2(x0,y0,x1,y1); } DrawTriangle(,,,,,);/* don't wait! * start processing buffered OpenGL routines */ glFlush ();}void init (void) { /* select clearing color */ glClearColor (0.0, 0.0, 0.0, 0.0);/* initialize viewing values */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, , 0, );}/* * Declare initial window size, position, and display mode * (single buffer and RGBA). Open window with "hello" * in its title bar. Call initialization routines. * Register callback function to display graphics. * Enter main loop and process events. */int main(int argc, char** argv){ cout<<"input x0,y0,x1,y1 :"<<endl; cin>>x0>>y0>>x1>>y1; glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); //设置窗口大小,以像素为单位 glutInitWindowSize (, ); glutInitWindowPosition (, ); glutCreateWindow ("hello");
希望能够帮助到你,望采纳,谢谢!
C++中输入一条线段(两个点),计算线段的长度。。。。急求大神!!!!!只需补充两个构造函数就OK了
点的构造函数
Point(int _x = 0, int _y = 0) : x(_x), y(_y)
{ }
线的构造函数
Line(int x1 = 0, int y1 = 0, int x2 = 0, int y2 = 0) : p1(x1, y1), p2(x2, y2)
{ }
2024-12-24 01:08
2024-12-24 00:30
2024-12-24 00:26
2024-12-23 23:02
2024-12-23 22:31