| Current Path : /var/www/html/venkat/check3/file/cg2013/pawan/ |
| Current File : /var/www/html/venkat/check3/file/cg2013/pawan/A_1_5.c |
#include <GL/glut.h>
void init(float r, float g, float b){
glClearColor(r,g,b,0.0); //set the background color
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,20,0.0,20);//x from 0 to 200, y from 0 to 200
}
void segment(void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0); //color of point
//float x[5],y[5];
//scanf("%f %f", &x1, &y1);
//scanf("%f %f", &x2, &y2);
float x[] = {8,2,11,6,5,4,12,9,6,1};
float y[] = {3,10,3,6,8,12,1,4,9,14};
int i,n = 10;
float sumx = 0,sumy = 0;
for(i = 0; i < n; i++){
sumx += x[i];
sumy += y[i];
}
float barx = sumx / n;
float bary = sumy / n;
float xy[n], sumxy = 0;
float sqx = 0;
float xx,yy;
for(i = 0; i < n; i++){
xx = x[i] - barx;
yy = y[i] - bary;
xy[i] = xx*yy;
sumxy += xy[i];
sqx += xx*xx;
}
float slop = sumxy / sqx;
float c = bary-slop*barx;
float barx1 = 110;
float bary1 = slop * barx1 + c;
glBegin(GL_POINTS);
for(i = 0; i < n; i++){
glVertex2i(x[i],y[i]);
}
glEnd();
glBegin(GL_LINES);
// for(i = 0; i < n; i++){
// glVertex2i(x[i],y[i]);
// }
//glVertex2i(barx,bary); // co-ordinates of point
glVertex2i(barx1,bary1); // co-ordinates of point
glVertex2i(barx,bary); // co-ordinates of point
glEnd();
glFlush();
}
void main(int argc, int *argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // GLUT_RGB stands for OpenGL Utility and RGB for red,green,blue
glutInitWindowSize(500, 500); //Inbuilt func to set the size of window
glutInitWindowPosition(250, 200);//inbuilt func to put the output window at position
glutCreateWindow("GCV Assignment 1 Que 5"); //Title of Window
init(0.0,0.0,0.0); //Function Called For background colors RGB respectively, vary between 0.0to 1.0 to see the differn background colors
glutDisplayFunc(segment); //function called segment
glutMainLoop();
}