Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question: Draw rectangles The user specifies the rectangle size by using the mouse. The user presses and holds the left mouse button in the drawing

Question:

Draw rectangles

The user specifies the rectangle size by using the mouse. The user presses and holds the left mouse button in the drawing area to determine a corner of the rectangle. The mouse is then moved to an opposite corner of the rectangle. Releasing the left mouse button draws the rectangle. When moving the mouse, an outline of a rectangle at the mouse position is showing (the robber band effect). Make sure that you are able to draw a rectangle in all directions from the initial point (i.e., not just to the bottom right corner).

Draw circles

The user presses the left mouse button to determine the center of the circle. Holding the left button and moving the mouse, an outline of the circle passing the mouse position is showing. Releasing the left mouse button gets a point on the circle. The midpoint circle algorithm should be used in drawing a circle.

Drawing styles

Use styles->filled or styles->outline to determine a filled object or outlined object.

Color selection

Use colors-> to select the color of object to draw. It needs to have at least three distinct colors, red, green and blue, but eight colors are recommended: black, red, yellow, green, cyan, blue, and magenta.

Edit

Edit submenu gives selections of graphics editing operations.

Select

Use Edit->select to get into object selection mode. Under this mode, when left mouse button is clicked, the program determines which object, if any, is underneath the mouse pointer. If there is more than one object under the mouse pointer, the program will select the top object. When an object is selected, its outline is high lighted (with different color) to indicate the object is selected.

Deselect

When an object is selected, use menu command Edit->deselect to deselect it.

Delete

When an object is selected, use Edit->delete to delete the selected object.

Pull to top / push to bottom

When an object is selected, use Edit->Pull to top / Edit->Push to bottom to bring the object to front/to send the object to back.

Move

When an object is selected, use Edit->move and press and hold the left mouse button at the selected object and then drag the mouse, the object will be moved according to the change in location between where the mouse button was pressed and where it was released. When the object is moving, it shows the moving.

Graphics file

Use Files->Save SVG File to save all the objects to SVG (XML) format file of name: output.svg

Use Files->Open SVG File to open the SVG file to open the output.svg file and import the object list.

Use Files->Save BMP File to save the graphics image to bitmap file named output.bmp, which can be viewed by bmp software.

Circle&Square artwork

Use your own SimpleDraw program to create an artwork consisting of circles and rectangles. Save your artwork in SVG and bitmap files.

Data visualization (bonus)

When choosing Data visualization->Bar, it draws dots of x, y data: 1.0,1.00 2.0,2.00 3.0,1.30 4.0,3.75 5.0,2.25 7.0,6.5 and computes and displays the line segment of the linear regression of the data.

Programming specifications

data structures

Design a unified data structure to represent graphics objects, i.e. rectangles and circles with color and style attributes.

Used a double linked list to hold a list of the graph objects, with data structure functions to support the SimpleDraw operations.

program structures

For better management of your the project, it is required to split the program into several files: for example,

object.hpp and object.cpp is for object structure and double linked list functions.

algorithm.hpp and algorithm.cpp contains all algorithm API and implementations

menu.hpp and menu.cpp is for menu function API and implementation

file.hpp and file.cpp is for file function API and implementation

dataview.hpp and dataview.cpp is for data visualization function API and implementation (bonus)

simpledraw.hpp for all header include, constants, and global variables, and simpledraw.cpp is main OpenGL program.

#include #include #include #include #include // C++ headers for file I/O #include

//#define some constants here #define MaxNumObj 100

// function prototypes void mainMenu(GLint menuOption);

// data structure for shape object, you can use this data structure in your project typedef struct shape { GLint x1, y1, x2, y2; // (x1, y1), (x2, y2) are two points that define the objects rectangle, rectangle, line segment, etc. GLint t, s; // t is used for types of object: 1 for rectangle, 2 for circle; s is for drawing styles: 1 for filled, 2 for outline GLfloat r, g, b; // RGB color } shape;

//in this start up program, array of shape structure is used. shape list[MaxNumObj];

//The assignment requires to use double linked list for storing objects typedef struct node { shape *sp; struct node *prev, *next; } node;

//global variables shape *sshapep; //selected shape object pointer shape tshape; //temporal shape variable used for moving object node *front = NULL, *back = NULL, *snodep=NULL; //pointers for double linked list. snodep is used for selected node pointer

// global variables GLsizei winWidth = 640, winHeight = 480; // variables and initial for width and height of display window GLint tool = 0, type = 1, style = 1, selection = 0, move = 0, xbegin, ybegin, numObj = 0; GLfloat red = 1.0, green = 0.0, blue = 0.0;

// function definition // initial function to set up OpenGL state variable other than default. void init(void) { glClearColor(1.0, 1.0, 1.0, 0.0); // Set display-window color to white glMatrixMode(GL_PROJECTION); gluOrtho2D(0.0, winWidth, winHeight, 0.0); glColor3f(1.0, 0.0, 0.0); glFlush(); }

// this function draws the list of objects void drawList() { glClear(GL_COLOR_BUFFER_BIT); // Clear display window. for (int i = 1; i <= numObj; i++) { glBegin(GL_LINES); glVertex2i(list[i].x1, list[i].y1); glVertex2i(list[i].x2, list[i].y2); glEnd(); }

glFlush(); }

void mouseDraw(GLint button, GLint action, GLint xMouse, GLint yMouse) { if (button == GLUT_LEFT_BUTTON) { if (action == GLUT_DOWN) { numObj++; list[numObj].x1 = xMouse; list[numObj].y1 = yMouse;

list[numObj].x2 = xMouse; list[numObj].y2 = yMouse;

printf("%d, %d ", list[numObj].x1, list[numObj].y1); // for debugging } else if (action == GLUT_UP) { list[numObj].x2 = xMouse; list[numObj].y2 = yMouse; printf("%d, %d ", list[numObj].x2, list[numObj].y2); // for debugging } }

glutPostRedisplay(); glFlush(); }

// this function takes the mouse position while moving mouse, use this for intermediate drawing void mouseMotion(GLint xMouse, GLint yMouse) { //xMouse, yMouse gives the moouse pointer coordinates while dragging with mouse button down. list[numObj].x2 = xMouse; list[numObj].y2 = yMouse;

glutPostRedisplay(); glFlush(); }

// reshape function for resized the window void winReshapeFcn(GLint newWidth, GLint newHeight) { /* Reset viewport and projection parameters */ glViewport(0, 0, newWidth, newHeight); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, GLdouble(newWidth), GLdouble(newHeight), 0.0); /* Reset display-window size parameters. */ winWidth = newWidth; winHeight = newHeight; drawList(); glFlush(); }

void mainMenu(GLint menuOption) { switch (menuOption) { case 1: numObj = 0; selection = 0; move = 0; break; case 2: exit(0); } glutPostRedisplay(); }

void colorSubMenu(GLint colorOption) { switch (colorOption) { case 1: { red = 0.0; green = 0.0; blue = 1.0; } break; case 2: { red = 0.0; green = 1.0; blue = 0.0; } break; case 3: { red = 1.0; green = 0.0; blue = 0.0; } break; case 4: { red = 1.0; green = 1.0; blue = 1.0; } } glutPostRedisplay(); }

// main function int main(int argc, char** argv) { setvbuf(stdout, NULL, _IONBF, 0); //for printing on Eclipse console setvbuf(stderr, NULL, _IONBF, 0);

glutInit(&argc, argv);

GLint color_subMenu = glutCreateMenu(colorSubMenu); glutAddMenuEntry("Red", 3); glutAddMenuEntry("Green", 2); glutAddMenuEntry("Blue", 1); glutAddMenuEntry("White", 4);

GLint Edit_subMenu = glutCreateMenu(colorSubMenu);

GLint Styles_subMenu = glutCreateMenu(colorSubMenu);

GLint Files_subMenu = glutCreateMenu(colorSubMenu);

glutCreateMenu(mainMenu); // Create main pop-up menu. glutAddMenuEntry(" New ", 1); glutAddSubMenu(" Colors ", color_subMenu); glutAddSubMenu(" Edit", Edit_subMenu); glutAddSubMenu(" Styles", Styles_subMenu); glutAddSubMenu(" Files", Files_subMenu); glutAddMenuEntry(" Quit", 5);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(100, 100); glutInitWindowSize(winWidth, winHeight); glutCreateWindow("SimpleDraw start up ");

init(); glutDisplayFunc(drawList); glutReshapeFunc(winReshapeFcn); glutMouseFunc(mouseDraw); glutMotionFunc(mouseMotion); glutAttachMenu(GLUT_RIGHT_BUTTON);//add right click menu glutMainLoop();

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions