【计算机图形学】画曲线和五角星

曲线 

#include "framework.h"  
#include <GL/glut.h>  
#define N 10000 //10000 points  
int line[N][2];//xy points  
int k = 0;//计数器 点数量  
int ww, hh;     // for display window width and height  
void Myinit(void);  
void Reshape(int w, int h);  
void Display(void);  
void myMouse(int button, int state, int x, int y)  
{  
    //鼠标左键按下—确定起始点  
    if (button == GLUT_LEFT_BUTTON && state = GLUT_DOWN) {  
        line[k][0] = x;  
        line[k][1] = hh-y;  
        k++;  
    }  
  
      //鼠标左键松开___画最后一个顶点,画线结束  
    if (button == GLUT_LEFT_BUTTON && state = GLUT_UP);  
    {  
        line[k][0] = x;  
        line[k][1] = hh - y;  
        k++;  
    }  
}  
void myMotion(int x, int y)  
{  
    //  鼠标移动__线画到哪  
  
}  
int APIENTRY _tWinMain(HINSTANCE hInstance,  
    HINSTANCE hPrevInstance,  
    LPTSTR    lpCmdLine,  
    int       nCmdShow)  
{  
    UNREFERENCED_PARAMETER(hPrevInstance);  
    UNREFERENCED_PARAMETER(lpCmdLine);  
  
    char* argv[] = { (char*)"hello ", (char*)" " };  
    int argc = 2; // must/should match the number of strings in argv  
  
    glutInit(&argc, argv);  //初始化GLUT库;  
    glutInitWindowSize(800, 600);  //设置显示窗口大小  
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);  //设置显示模式;(注意双缓冲)  
    glutCreateWindow("鼠标画线小程序演示"); // 创建显示窗口  
    Myinit();  
  
    glutDisplayFunc(Display);  //注册显示回调函数  
    glutReshapeFunc(Reshape);  //注册窗口改变回调函数  
  
    glutMouseFunc(myMouse);  
    glutMotionFunc(myMotion);  
  
    glutMainLoop();  //进入事件处理循环  
    return 0;  
}  
  
  
void Myinit(void)  
{  
    glClearColor(0.0, 0.0, 0.0, 0.0);  
    glLineWidth(3.0);  
}  
  
//渲染绘制子程序--------------------------------------------------------------------------  
void Display(void)  
{  
    glClear(GL_COLOR_BUFFER_BIT);   //刷新颜色缓冲区;  
    glBegin(GL_LINES);  
    glVertex2f(0, 0);  
    glVertex2f(ww, hh);  
    glEnd();  
    glutSwapBuffers();  //双缓冲的刷新模式;  
}  
  
//-----------------------------------------------  
void Reshape(int w, int h)  //窗口改变时自动获取显示窗口的宽w和高h  
{  
    glMatrixMode(GL_PROJECTION);  //投影矩阵模式  
    glLoadIdentity();     //矩阵堆栈清空  
    glViewport(0, 0, w, h);  //设置视区(0,0)到(w,h) 显示的图形  
    gluOrtho2D(0, w, 0, h);   //设置裁剪窗口大小(跟着视区)  
    ww = w;  
    hh = h;  
}  

五角星

 

#include "framework.h"  
#include <GL/glut.h>  

#define N 1000       //maximum line numbers
int ww,hh;     // for display window width and height
int line[N][4], k=0;  //for line's endpoint coordinates and line number

void Myinit(void);
void Reshape(int w, int h);
void myMouse(int button,int state,int x,int y);
void myMotion(int x,int y);
void Display(void);
void  drawlines();

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

	char* argv[] = { (char*)"hello ", (char*)" " };
	int argc = 2; // must/should match the number of strings in argv

	 glutInit(&argc, argv);  //初始化GLUT库;
     glutInitWindowSize(800, 600);  //设置显示窗口大小
     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);  //设置显示模式;(注意双缓冲)
     glutCreateWindow("鼠标画线小程序演示"); // 创建显示窗口
      Myinit();
       glutDisplayFunc(Display);  //注册显示回调函数
    glutMouseFunc(myMouse);    //注册鼠标按钮回调函数
    glutMotionFunc(myMotion);  //注册鼠标移动回调函数
    glutReshapeFunc(Reshape);  //注册窗口改变回调函数
       glutMainLoop();  //进入事件处理循环
      return 0;
}

void Myinit(void)
{
glClearColor(0.0,0.0,0.0,0.0);
glLineWidth(3.0);
}

//渲染绘制子程序--------------------------------------------------------------------------
void Display(void)
{
   glClear(GL_COLOR_BUFFER_BIT);   //刷新颜色缓冲区;
	drawlines();  //画线子程序;
    glutSwapBuffers();  //双缓冲的刷新模式;
}

//-----------------------------------------------
void Reshape(int w, int h)  //窗口改变时自动获取显示窗口的宽w和高h
{
   glMatrixMode(GL_PROJECTION);  //投影矩阵模式
   glLoadIdentity();     //矩阵堆栈清空
   glViewport(0, 0, w, h);  //设置视区大小
   gluOrtho2D(0, w, 0, h);   //设置裁剪窗口大小
   ww=w;
   hh=h;
}


//鼠标按钮响应事件..
void myMouse(int button,int state,int x,int y)
{
	if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
	{
	   line[k][0]=x;   //线段起点x坐标
	   line[k][1]=hh-y;  //线段终点y坐标
	}

	if(button==GLUT_LEFT_BUTTON&&state==GLUT_UP)
	{
			line[k][2]=x;   //线段起点x坐标
			line[k][3]=hh-y;   //线段终点y坐标
			k++;
		glutPostRedisplay();
	 }
}

//鼠标移动时获得鼠标移动中的坐标-----------------------------------------------------
void myMotion(int x,int y)
{
   	  //get the line's motion point
		line[k][2]=x;   //动态终点的x坐标
		line[k][3]=hh-y;  //动态终点的y坐标 
	glutPostRedisplay();
}

//画线子程序
void  drawlines()  
{
	   for(int i=0;i<=k;i++) //********
		{
        int x1 = line[i][0], x2 = line[i][2];  
        int y1 = line[i][1], y2 = line[i][3];  
        int a = (x2 - x1) / (1 + sin(PI / 10));  
  
        glBegin(GL_LINES);  
        glVertex2f(0 + x1, a * cos(PI / 10) + y1);//E  
        glVertex2f(-a * cos(PI / 5) + x1, -                            
        a * cos(PI / 5) * 1 / (tan(PI / 10)) + a * cos(PI / 10) + y1);//C  
        glVertex2f(-a * cos(PI / 5) + x1, -            
        a * cos(PI / 5) * 1 / (tan(PI / 10)) + a * cos(PI / 10) + y1);//C  
         glVertex2f(a+a*sin(PI/10)+x1,0 + y1);//A  
        glVertex2f(a + a * sin(PI / 10) + x1, 0 + y1);//A  
        glVertex2f(-a - a * sin(PI / 10) + x1, 0 + y1);//D  
        glVertex2f(-a - a * sin(PI / 10) + x1, 0 + y1);//D  
        glVertex2f(a * cos(PI / 5) + x1, -        
         a * cos(PI / 5) * 1 / (tan(PI / 10)) + a * cos(PI / 10) + y1);//B  
        glVertex2f(a * cos(PI / 5) + x1, -        
         a * cos(PI / 5) * 1 / (tan(PI / 10))+a*cos(PI/10)  + y1);//B  
        glVertex2f(0 + x1, a * cos(PI / 10) + y1);//E  
        glEnd();  
		}
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值