View Full Version : Rubber-band method drawing lines ??
Does anyone know how to use the rubber-band
method when drawing lines. You click the left mouse button to set the starting point and then makes the line stretches until you release the button.
The line should be like a rubber-band which can be stretched in the direction and the length you want.
PLEASE HELP ME email@example.com
03-29-2000, 01:53 AM
I guess you mean bezier curves...
try a tutorial on http://www.flipcode.com for that.
maybe someone here now knows to tell you some more?
As I understand, you want to click with the mouse for initial position, then the line will follow the mouse until you release the button?
You can do this with GLUT using glutMouseFunc() for handle mousebuttons, and glutMotionFunc() for handle mouse movements.
When you press a mousebutton, set the initial position.
When you move the mouse (with one button down) you draw the line from the initial position, to mouse position.
When you release the button, you can set the end position, and you got a line.
I think what theoden asked is drawing a line in rubber-band method ---- that means if drawing a line from p1 to p2, then you might go back to the position (p1+p2)/2, and you just want the line be drawn from p1 to (p1+p2)/2, you should erase the half from (p1+p2)/2 to p2, Will the way you suggested still work? MotionFunc will draw all the way you moved but only initial to end.
similarly, if I want to move a object from p1 to p2 using mouse to hold the object and do not draw the moving track, what function
and stratage will you recommend?
thank you very much!
You can use the functions I mentioned above. When glutMotionFunc() is called you get the coordinates of the mouse, then it's up to you to something with it. I said you draw a line from p1 to mouseposition. If you just want to draw the line from p1 halfway to p2, then calculate another value for p2, thats all.
p1 = valid point on screen
mp = mouseposition
p2.x = p1.x + (mp.x - p1.x)/2
p2.y = p1.y + (mp.y - p1.y)/2
draw line from p1 to p2
Most kind of mousemovements can be done with these functions. However, glutMouseFunc() will only be called when you move the mouse with one or more buttons pressed. There is also a function called glutPassiveMotionFunc() which work similar to glutMotionFunc(), but is called when you move your mouse without any buttons pressed.
01-17-2002, 10:46 AM
Can't you get the initial position of the mouse on a mousebutton down event, then get the final position of the mouse on the mousebutton up event?
In between you draw a rectangle every frame from original (mouse x, mouse y) to current (mouse x, mouse y).
Seems simple enough. On the mousebutton up event you have your bounding box which you can then project into the scene to see what you've actually selected.
[This message has been edited by Heaven (edited 01-17-2002).]
I have a rubbber band box example on my website. It is in MFC but the OpenGL code is the same.
//draw with open_gl & glut
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
typedef enum state
typedef struct point
point lines ;
for(int i=0; i<=numLines; i++)
void menufunc(int val)
glOrtho(-1, 1.0, -1, 1.0, -1.0, 1.0);
void reshape(int width, int height)
gluOrtho2D(0, width, 0,height);
void mouseclick(int button, int state,int x, int y)
if(button==GLUT_LEFT_BUTTON && state==GLUT_UP)
void mousedrag(int x, int y)
int main(int argc, char ** argv)
glutCreateWindow("My Drawing App");
glutPostRedisplay(); //added function for speed and clarity
Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.