Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <iostream>
- #include <sstream>
- #include <GL/freeglut.h>
- #include "Point.h"
- #define X_MIN_RANGE 0.0
- #define X_MAX_RANGE 10.0
- #define Y_MIN_RANGE 0.0
- #define Y_MAX_RANGE 10.0
- using namespace std;
- void initGLUT () {
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
- glutInitWindowSize(400, 400);
- glutInitWindowPosition(100, 100);
- glutCreateWindow("triunghi cu punct");
- }
- void drawAxis() {
- glLineWidth(2.0);
- glColor3f(0.0, 0.0, 0.0);
- glBegin(GL_LINES);
- glVertex2f(0.0, 0.0);
- glVertex2f(X_MAX_RANGE, 0.0);
- glVertex2f(0.0, 0.0);
- glVertex2f(0.0, Y_MAX_RANGE);
- for (int i = 0; i <= 10; i++) {
- glVertex2f(i, 0.0);
- glVertex2f(i, 0.3);
- glVertex2f(0.0, i);
- glVertex2f(0.3, i);
- }
- glEnd();
- }
- Point A, B, C, M;
- void draw() {
- glClearColor(1.0, 1.0, 1.0, 0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- drawAxis();
- glColor3f(0.0, 0.0, 0.0);
- glLineWidth(2.0);
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- glBegin(GL_TRIANGLES);
- glVertex2f(A.getX(), A.getY());
- glVertex2f(B.getX(), B.getY());
- glVertex2f(C.getX(), C.getY());
- glEnd();
- M.draw();
- glFlush();
- }
- float computeArea (Point A, Point B, Point C) {
- float xA = A.getX();
- float yA = A.getY();
- float xB = B.getX();
- float yB = B.getY();
- float xC = C.getX();
- float yC = C.getY();
- float delta = xA * (yB - yC) +
- xB * (yC - yA) +
- xC * (yA - yB);
- float area = abs(delta)/2;
- return area;
- }
- void checkIfInside () {
- float areaABC = computeArea (A, B, C);
- float areaAMB = computeArea (A, M, B);
- float areaBMC = computeArea (B, M, C);
- float areaCMA = computeArea (C, M, A);
- cout << areaABC << endl;
- cout << areaAMB + areaBMC + areaCMA << endl;
- cout << areaAMB * areaBMC * areaCMA << endl;
- M.setSize(5.0);
- if (areaABC == areaAMB + areaBMC + areaCMA) {
- // set M's color and size as green
- M.setColor(0.0, 1.0, 0.0);
- if (areaAMB * areaBMC * areaCMA == 0) {
- // set M's color as blue
- M.setColor(0.0, 0.0, 1.0);
- }
- }
- else {
- // set M's color and size as red
- M.setColor(1.0, 0.0, 0.0);
- }
- }
- void initPoints(int nargs, char* args[]) {
- stringstream converter;
- for (int i = 1; i < nargs; i++) {
- converter << ' ' << args[i];
- }
- GLfloat x, y;
- converter >> x >> y;
- A = Point(x, y);
- converter >> x >> y;
- B = Point(x, y);
- converter >> x >> y;
- C = Point(x, y);
- converter >> x >> y;
- M = Point(x, y);
- }
- void initGL () {
- glMatrixMode(GL_PROJECTION);
- gluOrtho2D(X_MIN_RANGE, X_MAX_RANGE, Y_MIN_RANGE, Y_MAX_RANGE);
- glutDisplayFunc(draw);
- }
- int main (int nargs, char* args[]) {
- glutInit(&nargs, args);
- initGLUT();
- initGL();
- initPoints(nargs, args);
- checkIfInside();
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement