OpenGL仿作橡皮筋技术

算是精仿吧,毕竟不一样,少了第二次点鼠标左键确认绘图,这个可以加上的:


#include <iostream>
#include <windows.h>
#include <GL/glut.h>

using namespace std ;
#pragma comment(lib, "glut32.lib")    
int flag =0;
int flagagain=0;
POINT start,down; 
void init(void)
{
 glClearColor(0.0, 0.0, 0.0, 0.0) ;
 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0) ;
}

void display(void) 
{
 glClear(GL_COLOR_BUFFER_BIT) ;
 if(flag)
 {
	glColor3f(1.0, 1.0, 1.0) ;
	glBegin(GL_LINES);
	GLfloat oldx = start.x*1.00/400;
	GLfloat oldy = start.y*1.00/400;
	GLfloat newx = down.x*1.00/400;
	GLfloat newy = down.y*1.00/400;
	glVertex2f(oldx,oldy);
	glVertex2f(newx,newy);
	glutSwapBuffers();  
 }
 glEnd() ;
 glFlush() ;
}
void motion(int x, int y) 
{
	if(!flag)
	{
		start.x = x;
		start.y = 400-y;
	}
	down.x = x;
	down.y = 400-y;
}
void mouse(int button, int state, int x, int y)  
{  
    if (button == GLUT_LEFT_BUTTON)  
    {  
        if (state == GLUT_DOWN)  
        {  
             glutMotionFunc(motion);//获得start
			 flag=1;
        }  
    }  
}  
int main(int argc, char **argv)
{
 glutInit(&argc, argv) ;
 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);  
 glutInitWindowSize(400, 400) ;
 glutCreateWindow("I love OpenGl") ;
 init() ;
 glutDisplayFunc(display) ;
 glutMouseFunc(mouse);
 glutPassiveMotionFunc(motion);
 glutIdleFunc(display);  
 glutMainLoop();  
 return 0 ;
}

//喏 空心圆


#include <iostream>
#include <windows.h>
#include <GL/glut.h>
#include<math.h>
using namespace std ;
#define pi 3.1415926535843721
#pragma comment(lib, "glut32.lib")    
int flag =0;
int flagagain=0;
int n;
POINT start,down; 
float GetLength(POINT start,POINT down)
{
	return sqrt((start.x*1.00/400-down.x*1.00/400)*(start.x*1.00/400-down.x*1.00/400)+(start.y*1.00/400-down.y*1.00/400)*(start.y*1.00/400-down.y*1.00/400));
}
void init(void)
{
 glClearColor(0.0, 0.0, 0.0, 0.0) ;
 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0) ;
}
void display(void) 
{
 glClear(GL_COLOR_BUFFER_BIT) ;
 if(flag)
 {
	glColor3f(1.0, 1.0, 1.0) ;
	glBegin(GL_LINES);
	GLfloat oldx = start.x*1.00/400;
	GLfloat oldy = start.y*1.00/400;
	GLfloat newx = down.x*1.00/400;
	GLfloat newy = down.y*1.00/400;
	glVertex2f(oldx,oldy);
	glVertex2f(newx,newy);
	glEnd();
	glBegin(GL_LINE_STRIP); 
	glColor3f(1.0, 1.0, 1.0) ;
	n = 100;
	float R = GetLength(start,down);//半径
	cout<<R<<endl;//输出半径
	for (int i = 0; i < n; i++)
	{
		glVertex2f(R*sin(2 * pi / n*i)+oldx, R*cos(2 * pi / n*i)+oldy);//平移到  以原来鼠标 左 键  为原点
	}
	glEnd();


	glutSwapBuffers();  
 }
 glEnd() ;
 glFlush() ;
}
void motion(int x, int y) 
{
	if(!flag)
	{
		start.x = x;
		start.y = 400-y;
	}
	down.x = x;
	down.y = 400-y;
}
void mouse(int button, int state, int x, int y)  
{  
    if (button == GLUT_LEFT_BUTTON)  
    {  
        if (state == GLUT_DOWN)  
        {  
             glutMotionFunc(motion);//获得start
			 flag=1;
        }  
    }  
}  
int main(int argc, char **argv)
{
 glutInit(&argc, argv) ;
 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);  
 glutInitWindowSize(400, 400) ;
 glutCreateWindow("I love OpenGl") ;
 init() ;
 glutDisplayFunc(display) ;
 glutMouseFunc(mouse);
 glutPassiveMotionFunc(motion);
 glutIdleFunc(display);  
 glutMainLoop();  
 return 0 ;
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值