From e440a5032249019c81caaafab8547a708373d5f7 Mon Sep 17 00:00:00 2001 From: AntonGerasimov Date: Sun, 17 May 2015 20:04:54 +0300 Subject: [PATCH 01/19] Add first --- 051315/first/device.h | 1408 +++++++++++++++++++++++++++++++++++++++++ 051315/first/first.cc | 60 ++ 051315/first/first.h | 68 ++ 051315/first/main.cc | 289 +++++++++ 051315/first/ray.h | 388 ++++++++++++ 5 files changed, 2213 insertions(+) create mode 100644 051315/first/device.h create mode 100644 051315/first/first.cc create mode 100644 051315/first/first.h create mode 100644 051315/first/main.cc create mode 100644 051315/first/ray.h diff --git a/051315/first/device.h b/051315/first/device.h new file mode 100644 index 0000000..75cf91d --- /dev/null +++ b/051315/first/device.h @@ -0,0 +1,1408 @@ +#include +#include +#include "math.h" +#include "ray.h" +using namespace std; + +#define PI 3.14159265 +#define NUMBER 10 // HERE YOU CAN CHANGE NUMBER OF CREATED RAYS {360 : (NUMBBER - 1)} + + +float LenVect(point *p1, point *p2) { + return sqrt(Sqr(p2->x - p1->x) + Sqr(p2->y - p1->y)); +} + +float Scalar(point *p1, point *p2) { + return p1->x * p2->x + p1->y * p2->y; +} + +point *SumVect(point *p1, point *p2) { + point *sum_p = new point; + sum_p->x = p1->x + p2->x; + sum_p->y = p1->y + p2->y; + return sum_p; +} + +point *SubVect(point *p1, point *p2) { + point *sub_p = new point; + sub_p->x = p2->x - p1->x; + sub_p->y = p2->y - p1->y; + return sub_p; +} +class Prism; + +int orient (float x1, float y1, float x2, float y2, float x3, float y3, float this_deg) {// x1,y1; x2,y2 - line x3,y3 - point + // cout << "x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 << " x3 = " << x1 << " y3 = " << y3 << "\n"; + if (this_deg == 0){ + if (y3 <= y2){ + + // cout << "trying 1 \n"; + return 1; + } + else{ + // cout << "trying -1 \n"; + return -1; + } + } + else { + if (((x3 -x1) * (y2 - y1) - (y3 -y1) * (x2 - x1)) >= 0) + return 1; + else + return -1; + } +} + +class Device{ +public: + float x1, x2, y1, y2, deg, f ; // y1 - up y2 - down x1 - left x2 - right + virtual void change_direction(RAY * r, point * p) const = 0; + //virtual const char *getID() const = 0; + virtual int getID() + { + return -1; + } + virtual point * cross_point (RAY * r) const = 0; +}; +class Disc : public Device{ // n > 1 !!! +public: + float w,l,n, x3,y3, x4, y4, deg; // w - width + +public: + Disc ( float x, float y, float l_0, float w_0, float deg_0, float n_0){ + float x_1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180) - (float) w_0 / 2 * cos (deg_0 * PI / 180) ; + float x_2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180) - (float) w_0 / 2 * cos (deg_0 * PI / 180) ; + float y_1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180) + (float) w_0 / 2 * sin (deg_0 * PI / 180) ; + float y_2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180) + (float) w_0 / 2 * sin (deg_0 * PI / 180) ; + + float x_3 = x_1 + (float)(w_0) * cos (deg_0 * PI / 180); + float x_4 = x_2 + (float)(w_0) * cos (deg_0 * PI / 180); + float y_3 = y_1 - (float)(w_0) * sin (deg_0 * PI / 180); + float y_4 = y_2 - (float)(w_0) * sin (deg_0 * PI / 180); + + deg = deg_0; + w = w_0; + l = l_0; + n = n_0; + + x1 = x_1 * cos (deg * PI / 180) - y_1 * sin (deg * PI / 180); + x2 = x_2 * cos (deg * PI / 180) - y_2 * sin (deg * PI / 180); + x3 = x_3 * cos (deg * PI / 180) - y_3 * sin (deg * PI / 180); + x4 = x_4 * cos (deg * PI / 180) - y_4 * sin (deg * PI / 180); + + + y1 = x_1 * sin (deg * PI / 180) + y_1 * cos (deg * PI / 180); + y2 = x_2 * sin (deg * PI / 180) + y_2 * cos (deg * PI / 180); + y3 = x_3 * sin (deg * PI / 180) + y_3 * cos (deg * PI / 180); + y4 = x_4 * sin (deg * PI / 180) + y_4 * cos (deg * PI / 180); + + + cout << "x_1 = " << x_1 << " x_2 = " << x_2 << " x_3 = " << x_3 << " x_4 = " << x_4 << " y_1 = " << y_1 << " y_2 = " << y_2 << " y_3 = " << y_3 << " y_4 = " << y_4 << "\n"; + + cout << "x1 = " << x1 << " x2 = " << x2 << " x3 = " << x3 << " x4 = " << x4 << " y1 = " << y1 << " y2 = " << y2 << " y3 = " << y3 << " y4 = " << y4 << "\n"; + + + + } + int getID() + { + return 4; + } + point * cross_point (RAY * r) const { + + point * p = new point (); + float p_x, p_y; + float r_x, r_y; + r_x = r->x; + r_y = r->y; + r->x = r_x * cos (this->deg * PI / 180) - r_y * sin (this->deg * PI / 180); + r->y = r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + if (r->deg >= this->deg) + r->deg = r->deg - this->deg; + else + r->deg = 360 + r->deg -this->deg; + cout << "r->x = " << r->x << " r->y = " << r->y << " r->deg = " << r->deg << "\n"; + + + + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))) { + cout << "h_0\n"; + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + cout << "det = " << det << "\n"; + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + cout << "det_1 = " << det_1 << "\n"; + + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + cout << "det_2 = " << det_2 << "\n"; + + + p->x = (float)det_1/det; + p->y = (float)det_2/det; + + cout << "p->x = " << p->x << " p->y = " << p->y << "\n"; + cout << "x1 = " << x1 << " x2 = " << x2 << " y1 = " << y1 << " y2 = " << y2 << "\n"; + + + if ( ( p->y >= y1 ) && ( p->y <= y2 )) + { + p_x = p->x; + p_y = p->y; + p->x = p_x * cos (this->deg * PI / 180) + p_y * sin (this->deg * PI / 180); + p->y = - p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); + r_x = r->x; + r_y = r->y; + r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); + r->y = -r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + return p; + } + } + if ((orient (this->x3,this->y3,this->x4,this->y4,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x3 - this->x4); + float det_1 = this->y2 * this->x3 - this->y1 * this->x4 - (this->x3 - this->x4) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x3 - this->y1 * this->x4) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->y >= y1 ) && ( p->y <= y2 )) + { + p_x = p->x; + p_y = p->y; + p->x = p_x * cos (this->deg * PI / 180) + p_y * sin (this->deg * PI / 180); + p->y = - p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); + r_x = r->x; + r_y = r->y; + r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); + r->y = -r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + return p; + } + } + + if ((orient (this->x1,this->y1,this->x3,this->y3,r->x,r->y,1) > 0) && ((r->deg >= 180) && (r->deg <= 360))) { + //cout << "this->y1 = "<< this->y1 << "\n"; + float det = this->x3 - this->x1 - (float) cos (r->deg * PI / 180) / sin (r->deg * PI / 180) * (this->y1 - this->y3); + //cout << "det = " << det << "\n"; + float det_1 = this->x3 * this->y1 - this->x1 * this->y3 - (this->y1 - this->y3) * (r->x + (float) cos (r->deg * PI / 180) / sin (r->deg * PI / 180) * r->y); + //cout << "det_1 = " << det_1 << "\n"; + float det_2 = (this->x3 - this->x1) * (r->x + (float) cos (r->deg * PI / 180) / sin (r->deg * PI / 180) * r->y) - (this->x3 * this->y1 - this->x1 * this->y3) * (float) cos (r->deg * PI / 180) / sin (r->deg * PI / 180); + cout << "det_2 = " << det_2 << "\n"; + p->x = (float)det_1/det; + p->y = (float)det_2/det; + float t = p->y; + p->y = p->x; + p->x = t; + //cout << "p->x = " << p->x << "\n"; + //cout << "p->y = " << p->y << "\n"; + if (( p->x >= x1 ) && ( p->x <= x3 )) + { + p_x = p->x; + p_y = p->y; + p->x = p_x * cos (this->deg * PI / 180) + p_y * sin (this->deg * PI / 180); + p->y = - p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); + r_x = r->x; + r_y = r->y; + r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); + r->y = -r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + return p; + } + } + + if ((orient (this->x2,this->y2,this->x4,this->y4,r->x,r->y,1) < 0) && ((r->deg >= 0) && (r->deg <= 180))) { + + float det = this->x4 - this->x2 - (float) cos (r->deg * PI / 180) / sin (r->deg * PI / 180) * (this->y2 - this->y4); + float det_1 = this->x4 * this->y2 - this->x2 * this->y4 - (this->y2 - this->y4) * (r->x + (float) cos (r->deg * PI / 180) / sin (r->deg * PI / 180) * r->y); + float det_2 = (this->x4 - this->x2) * (r->x + (float) cos (r->deg * PI / 180) / sin (r->deg * PI / 180) * r->y) - (this->x4 * this->y2 - this->x2 * this->y4) * (float) cos (r->deg * PI / 180) / sin (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + float t = p->y; + p->y = p->x; + p->x = t; + if (( p->x >= x2 ) && ( p->x <= x4 )) + { + p_x = p->x; + p_y = p->y; + p->x = p_x * cos (this->deg * PI / 180) + p_y * sin (this->deg * PI / 180); + p->y = - p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); + r_x = r->x; + r_y = r->y; + r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); + r->y = -r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + return p; + } + } + + + + return NULL; + } + + + void change_direction(RAY * r, point * p ) const { + float p_x, p_y, r_x,r_y; + p_x = p->x; + p_y = p->y; + p->x = p_x * cos (this->deg * PI / 180) - p_y * sin (this->deg * PI / 180); + p->y = p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); + r_x = r->x; + r_y = r->y; + r->x = r_x * cos (this->deg * PI / 180) - r_y * sin (this->deg * PI / 180); + r->y = r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + + point * new_p = p; + float deg = 0; + + if ((p->y <= this->y2) && (p->y >= this->y1)) { + float beta = asin (fabs (sin (r->deg * PI / 180) / this->n) )* 180 / PI; + if ((((fabs((this->y1 - p->y)) * tan ((90-beta) * PI / 180)) >= this->w) && (r->y >= p->y)) || (((fabs((this->y2 - p->y)) * tan ((90-beta) * PI / 180)) >= this->w) && (r->y <= p->y))) { + if (r->x < p->x){ + if (r->y >= p->y) { + new_p->x = p->x + this->w; + new_p->y = p->y - this->w * tan ( beta * PI / 180); + } + else { + beta = fabs (beta); + new_p->x = p->x + this->w; + new_p->y = p->y + this->w * tan ( beta * PI / 180); + } + } + + else { + if (r->y >= p->y) { + new_p->x = p->x - this->w; + new_p->y = p->y - this->w * tan ( beta * PI / 180); + } + else { + beta = fabs (beta); + new_p->x = p->x - this->w; + new_p->y = p->y + this->w * tan ( beta * PI / 180); + } + } + deg = r->deg; + } + else { + //cout << "yes \n" ; + if (r->x < p->x){ + if (r->y >= p->y) { + new_p->x = p->x + fabs((this->y1 - p->y)) * tan ((90-beta) * PI / 180); + new_p->y = this->y1; + if ((cos (beta * PI / 180) * this->n) <= 1) + deg = 90 - asin (cos (beta * PI / 180) * this->n) * 180 / PI; + else + deg = 0; + } + else { + beta = fabs (beta); + new_p->x = p->x + fabs((this->y2 - p->y)) * tan ((90-beta) * PI / 180); + new_p->y = this->y2; + if ((cos (beta * PI / 180) * this->n) <= 1) + deg = 270 + asin (cos (beta * PI / 180) * this->n) * 180 / PI; + else + deg = 360; + } + } + + else { + if (r->y >= p->y) { + new_p->x = p->x - fabs((this->y1 - p->y)) * tan ((90-beta) * PI / 180); + new_p->y = this->y1; + if ((cos (beta * PI / 180) * this->n) <= 1) + deg = 90 + asin (cos (beta * PI / 180) * this->n) * 180 / PI; + else + deg = 180; + } + else { + beta = fabs (beta); + new_p->x = p->x - fabs((this->y2 - p->y)) * tan ((90-beta) * PI / 180); + new_p->y = this->y2; + if ((cos (beta * PI / 180) * this->n) <= 1) + deg = 270 - asin (cos (beta * PI / 180) * this->n) * 180 / PI; + else + deg = 180; + } + } + } + } + else { + float beta = asin (fabs (cos (r->deg * PI / 180) / this->n) )* 180 / PI; + if ((((fabs((this->x1 - p->x)) * tan ((90-beta) * PI / 180)) >= this->l) && (r->x >= p->x)) || (((fabs((this->x3 - p->x)) * tan ((90-beta) * PI / 180)) >= this->l) && (r->x <= p->x))) { + float beta = asin (fabs (cos (r->deg * PI / 180) / this->n) )* 180 / PI; + if (r->y < p->y){ + if (r->x >= p->x) { + new_p->y = p->y + this->l; + new_p->x = p->x - this->l * tan ( beta * PI / 180); + } + else { + beta = fabs (beta); + new_p->y = p->y + this->l; + new_p->x = p->x + this->l * tan ( beta * PI / 180); + } + } + + else { + if (r->x >= p->x) { + new_p->y = p->y - this->l; + new_p->x = p->x - this->l * tan ( beta * PI / 180); + } + else { + beta = fabs (beta); + new_p->y = p->y - this->l; + new_p->x = p->x + this->l * tan ( beta * PI / 180); + } + } + deg = r->deg; + } + else { + //cout << "yes \n" ; + if (r->y < p->y){ + if (r->x >= p->x) { + new_p->y = p->y + fabs((this->x1 - p->x)) * tan ((90-beta) * PI / 180); + new_p->x = this->x1; + if ((cos (beta * PI / 180) * this->n) <= 1) + deg = 180 + asin (cos (beta * PI / 180) * this->n) * 180 / PI; + else + deg = 270; + } + else { + beta = fabs (beta); + new_p->y = p->y + fabs((this->x3 - p->x)) * tan ((90-beta) * PI / 180); + new_p->x = this->x3; + if ((cos (beta * PI / 180) * this->n) <= 1) + deg = 360 - asin (cos (beta * PI / 180) * this->n) * 180 / PI; + else + deg = 270; + } + } + // HERE YOU STOPPED + else { + if (r->x >= p->x) { + new_p->y = p->y - fabs((this->x1 - p->x)) * tan ((90-beta) * PI / 180); + new_p->x = this->x1; + if ((cos (beta * PI / 180) * this->n) <= 1) + deg = 180 - asin (cos (beta * PI / 180) * this->n) * 180 / PI; + else + deg = 90; + } + else { + beta = fabs (beta); + new_p->y = p->y - fabs((this->x3 - p->x)) * tan ((90-beta) * PI / 180); + new_p->x = this->x3; + if ((cos (beta * PI / 180) * this->n) <= 1) + deg = asin (cos (beta * PI / 180) * this->n) * 180 / PI; + else + deg = 90; + } + } + } + } + //cout << "beta = " << beta << "\n"; + r->x = new_p->x * cos (this->deg * PI / 180) + new_p->y * sin (this->deg * PI / 180) ; + r->y = new_p->y * cos (this->deg * PI / 180) - new_p->x * sin (this->deg * PI / 180); + r->deg = deg + this->deg; + if (r->deg > 360) + r->deg = r->deg - 360; + + } + + + ~Disc () + { + cout << "Destructure of the Disc" << "\n"; + } + + +}; + + + + +/* +class Lens_ras : public Device{ +public: + float deg, f, l; + +public: + Lens_ras ( float x, float y, float l_0, float deg_0, float f_0){ //deg from vertical 0 <= deg < 90 !!! against hour ; + x1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180) ; + x2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180); + y1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180); + y2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180); + l = l_0; + f = f_0; + deg = deg_0; + } + point * cross_point (RAY * r) const { + point * p = new point (); + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))){ + cout << "c_here_1 \n"; + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { + cout << "c_here_2 \n"; + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + return NULL; + } + + void change_direction(RAY * r, point * p ) const + { + float l_1 = 0; + float alpha = 0; + float alpha_r = 0; + float c = 0 ; + float length = (float) sqrt ((r->x - (this->x1 + this->x2)/2) * (r->x - (this->x1 + this->x2)/2)+ (r->y - (this->y1 + this->y2)/2) * (r->y - (this->y1 + this->y2)/2)) ; + float line_tg = fabs ((float)((r->y - (this->y1 + this->y2)/2) / ((this->x1 + this->x2)/2 - r->x))) ; + //cout << " line_tg_prev = " << line_tg << "\n"; + float line_deg = atan (line_tg) * 180 / PI; + cout << "line_deg_prev = " << line_deg <<"\n"; + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) { + if (r->y <= (this->y1 + this->y2)/2) + line_deg = this->deg + line_deg; + else + line_deg = fabs (line_deg - this->deg); + } + else { + if (r->y >= (this->y1 + this->y2)/2) + line_deg = this->deg + line_deg; + else + line_deg = fabs (line_deg - this->deg); + } + + cout << "line_deg = " << line_deg << "\n"; + line_tg = tan (line_deg * PI / 180); + + //cout << " line_tg = " << line_tg << "\n"; + float a = length * cos (atan (line_tg)); + //cout << " a = " << a << "\n"; + float b = (float) (this->f * a / (a + this->f)); + //cout << " b = " << b << "\n"; + c = sqrt ((p->x - (this->x1 + this->x2)/2) * (p->x - (this->x1 + this->x2)/2)+ (p->y - (this->y1 + this->y2)/2) * (p->y - (this->y1 + this->y2)/2)) ; + + if (((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) || ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0))) { // точка пересечения и координата луча по разные стороны от опт.оси + l_1 = c + (float) b * line_tg; + cout << "here \n"; + alpha = atan ((float) (l_1 / b)) * 180 / PI; + alpha_r = 180 -alpha ; + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2/cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2/cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) { //точка координата луча выше точка пересечения ниже + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << " here_0 \n"; + } + + } + else { //точка персечкения и полоожение луча по одну сторону от опт.оси + l_1 = fabs (c - (float) b * line_tg); + alpha = atan ((float) (l_1 / b)) * 180 / PI; + alpha_r = 180 -alpha ; + cout << "here_here \n"; + if ((orient (this->x1, this->y1 + this->l / 2/cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)){ // луч выше опт.оси точка пересечения под всмогат.линией сверху + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "here_1 \n"; + + } + + + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) { // луч ниже опт.оси точка пересечения над всмогат.линией снизу + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "alpha =" << alpha << "\n"; + cout << "here_2 \n"; + } + + } + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) + r->deg = alpha ; + else + r->deg = alpha_r; + + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) { + if ((r->deg + this->deg - 360) > 0) + r->deg = (r->deg + this->deg - 360); + else + r->deg = r->deg + this->deg; + } + else { + + r->deg = r->deg + this->deg; + } + + r->x = p->x; + r->y = p->y; + } + const char *getID() const{ + return "Lens_ras"; + } + + ~Lens_ras () + { + cout << "Destructure of the Lens_ras" << "\n"; + } + + +}; +*/ + + +class Lens : public Device{ +public: + float deg, f, l; + +public: + Lens( float x, float y, float l_0, float deg_0, float f_0){ //deg from vertical 0 <= deg < 90 !!! against hour ; + x1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180) ; + x2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180); + y1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180); + y2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180); + l = l_0; + f = f_0; + deg = deg_0; + } + point * cross_point (RAY * r) const { + point * p = new point (); + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))){ + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + cout << "p->x = " << p->x << "\n" ; + p->y = (float)det_2/det; + cout << "p->x = " << p->x << "\n" ; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + return NULL; + } + + void change_direction(RAY * r, point * p ) const + { + float l_1 = 0; + float alpha = 0; + float alpha_r = 0; + float c = 0 ; + float length = (float) sqrt ((r->x - (this->x1 + this->x2)/2) * (r->x - (this->x1 + this->x2)/2)+ (r->y - (this->y1 + this->y2)/2) * (r->y - (this->y1 + this->y2)/2)) ; + float line_tg = fabs ((float)((r->y - (this->y1 + this->y2)/2) / ((this->x1 + this->x2)/2 - r->x))) ; + //cout << " line_tg_prev = " << line_tg << "\n"; + float line_deg = atan (line_tg) * 180 / PI; + cout << "line_deg_prev = " << line_deg <<"\n"; + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) { + if (r->y <= (this->y1 + this->y2)/2) + line_deg = this->deg + line_deg; + else + line_deg = fabs (line_deg - this->deg); + } + else { + if (r->y >= (this->y1 + this->y2)/2) + line_deg = this->deg + line_deg; + else + line_deg = fabs (line_deg - this->deg); + } + + cout << "line_deg = " << line_deg << "\n"; + line_tg = tan (line_deg * PI / 180); + + //cout << " line_tg = " << line_tg << "\n"; + float a = length * cos (atan (line_tg)); + //cout << " a = " << a << "\n"; + if (a != this->f){ + if (a > this->f) { + float b = (float) (this->f * a / (a - this->f)); + cout << " b = " << b << "\n"; + c = sqrt ((p->x - (this->x1 + this->x2)/2) * (p->x - (this->x1 + this->x2)/2)+ (p->y - (this->y1 + this->y2)/2) * (p->y - (this->y1 + this->y2)/2)) ; + + if (((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - l / 2 / cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0)) || ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0))) { // точка пересечения и координата луча выше или ниже опт.оси + l_1 = c + (float) b * line_tg; + cout << "here \n"; + alpha = atan ((float) (l_1 / b)) * 180 / PI; + alpha_r = 180 -alpha ; + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2/cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2/cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0)) { //точка пересечения и координата луча выше + + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + + cout << " here_0 \n"; + } + + } + else { //точка персечкения и полоожение луча по разные стороны от опт.оси + l_1 = fabs (c - (float) b * line_tg); + alpha = atan ((float) (l_1 / b)) * 180 / PI; + cout << "here \n"; + if ((orient (this->x1, this->y1 + this->l / 2/cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0)){ // луч выше опт.оси точка пересечения под всмогат.линией снизу + + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "here_1 \n"; + + } + + + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0)) { // луч ниже опт.оси точка пересечения за всмогат.линией сверху + + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "alpha =" << alpha << "\n"; + cout << "here_2 \n"; + } + + } + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) + r->deg = alpha ; + else + r->deg = alpha_r; + } + else { // a < f + cout << "here_3\n"; + float b = fabs((float) (this->f * a / (a - this->f))); + //cout << " b = " << b << "\n"; + c = sqrt ((p->x - (this->x1 + this->x2)/2) * (p->x - (this->x1 + this->x2)/2)+ (p->y - (this->y1 + this->y2)/2) * (p->y - (this->y1 + this->y2)/2)) ; + cout << "c = " << c << "\n"; + + if (((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) || ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0))) { // точка пересечения и координата луча по разные стороны от опт.оси + cout << "here_4\n"; + l_1 = c + (float) b * line_tg; + alpha = atan ((float) (l_1 / b)) * 180 / PI; + alpha_r = 180 - alpha; + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)){ // координата луча выше опт.оси точка персечения ниже опт.оси + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "here_5\n"; + cout << "alpha = " << alpha << "\n"; + + } + + } + else { // по одну сторону + cout << "here_6\n"; + l_1 = fabs (c - (float) b * line_tg); + alpha = atan ((float) (l_1 / b)) * 180 / PI; + alpha_r = 180 -alpha ; + /*cout << " x1 = " << this->x1 << "\n"; + cout << " y1 = " << this->y1 + (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180) << "\n"; + cout << " x2 = " << this->x2 << "\n"; + cout << " y2 = " << this->y2 - (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180) << "\n"; + cout << " x3 = " << p->x << "\n"; + cout << " y3 = " << p->y << "\n";*/ + + + + + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) { // луч выше опт.оси пересечение под вспомагат.линии сверху + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "here_7\n"; + + } + + + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) { // луч ниже опт.оси пересечение за вспомагат.линии снизу + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "here_8\n"; + + } + } + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) + r->deg = alpha ; + else + r->deg = alpha_r; + } + } + else { // a = f + alpha = atan (line_tg) * 180 / PI; + alpha_r = alpha; + if (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0){ + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + } + + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) + r->deg = alpha ; + else + r->deg = alpha_r; + + } + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) { + if ((r->deg + this->deg - 360) > 0) + r->deg = (r->deg + this->deg - 360); + else + r->deg = r->deg + this->deg; + } + else { + + r->deg = r->deg + this->deg; + } + + r->x = p->x; + r->y = p->y; + } + const char *getID() const{ + return "Lens"; + } + + ~Lens () + { + cout << "Destructure of the Lens" << "\n"; + } +}; + + + +class Lens_wide : public Device{ +public: + float deg, l,r1,r2,n,d; + +public: + Lens_wide( float x, float y, float l_0, float deg_0, float r1_0, float r2_0, float n_0, float d_0){ //deg from vertical 0 <= deg < 90 !!! against hour ; + x1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180) ; + x2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180); + y1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180); + y2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180); + l = l_0; + r1 = r1_0; + r2 = r2_0; + n = n_0; + d = d_0; + deg = deg_0; + } + point * cross_point (RAY * r) const { + point * p = new point (); + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))){ + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + cout << "p->x = " << p->x << "\n" ; + p->y = (float)det_2/det; + cout << "p->x = " << p->x << "\n" ; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + return NULL; + } + + void change_direction(RAY * r, point * p ) const + { + float l_1 = 0; + float alpha = 0; + float alpha_r = 0; + float c = 0 ; + float length = (float) sqrt ((r->x - (this->x1 + this->x2)/2) * (r->x - (this->x1 + this->x2)/2)+ (r->y - (this->y1 + this->y2)/2) * (r->y - (this->y1 + this->y2)/2)) ; + float line_tg = fabs ((float)((r->y - (this->y1 + this->y2)/2) / ((this->x1 + this->x2)/2 - r->x))) ; + //cout << " line_tg_prev = " << line_tg << "\n"; + float line_deg = atan (line_tg) * 180 / PI; + cout << "line_deg_prev = " << line_deg <<"\n"; + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) { + if (r->y <= (this->y1 + this->y2)/2) + line_deg = this->deg + line_deg; + else + line_deg = fabs (line_deg - this->deg); + } + else { + if (r->y >= (this->y1 + this->y2)/2) + line_deg = this->deg + line_deg; + else + line_deg = fabs (line_deg - this->deg); + } + + cout << "line_deg = " << line_deg << "\n"; + line_tg = tan (line_deg * PI / 180); + + //cout << " line_tg = " << line_tg << "\n"; + float a = length * cos (atan (line_tg)); + //cout << " a = " << a << "\n"; + float f = 0; + if (orient (this->x1, this->y1, this->x2, this->y2, r->x, r->y,1) < 0) + f = (float) 1 / ((this->n -1) * ((float)1/this->r1 - (float)1/this->r2 + (float) ((this->n-1) * this->d) / (this->n * this->r1 *this->r2))) ; + else + f = (float) 1 / ((this->n -1) * ((float)1/this->r2 - (float)1/this->r1 + (float) ((this->n-1) * this->d) / (this->n * this->r1 *this->r2))) ; + if (a != this->f){ + if (a > f) { + float b = (float) (f * a / (a - f)); + cout << " b = " << b << "\n"; + c = sqrt ((p->x - (this->x1 + this->x2)/2) * (p->x - (this->x1 + this->x2)/2)+ (p->y - (this->y1 + this->y2)/2) * (p->y - (this->y1 + this->y2)/2)) ; + + if (((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - l / 2 / cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0)) || ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0))) { // точка пересечения и координата луча выше или ниже опт.оси + l_1 = c + (float) b * line_tg; + cout << "here \n"; + alpha = atan ((float) (l_1 / b)) * 180 / PI; + alpha_r = 180 -alpha ; + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2/cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2/cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0)) { //точка пересечения и координата луча выше + + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + + cout << " here_0 \n"; + } + + } + else { //точка персечкения и полоожение луча по разные стороны от опт.оси + l_1 = fabs (c - (float) b * line_tg); + alpha = atan ((float) (l_1 / b)) * 180 / PI; + cout << "here \n"; + if ((orient (this->x1, this->y1 + this->l / 2/cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0)){ // луч выше опт.оси точка пересечения под всмогат.линией снизу + + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "here_1 \n"; + + } + + + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0)) { // луч ниже опт.оси точка пересечения за всмогат.линией сверху + + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "alpha =" << alpha << "\n"; + cout << "here_2 \n"; + } + + } + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) + r->deg = alpha ; + else + r->deg = alpha_r; + } + else { // a < f + cout << "here_3\n"; + float b = fabs((float) (f * a / (a - f))); + //cout << " b = " << b << "\n"; + c = sqrt ((p->x - (this->x1 + this->x2)/2) * (p->x - (this->x1 + this->x2)/2)+ (p->y - (this->y1 + this->y2)/2) * (p->y - (this->y1 + this->y2)/2)) ; + cout << "c = " << c << "\n"; + + if (((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) || ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0))) { // точка пересечения и координата луча по разные стороны от опт.оси + cout << "here_4\n"; + l_1 = c + (float) b * line_tg; + alpha = atan ((float) (l_1 / b)) * 180 / PI; + alpha_r = 180 - alpha; + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)){ // координата луча выше опт.оси точка персечения ниже опт.оси + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "here_5\n"; + cout << "alpha = " << alpha << "\n"; + + } + + } + else { // по одну сторону + cout << "here_6\n"; + l_1 = fabs (c - (float) b * line_tg); + alpha = atan ((float) (l_1 / b)) * 180 / PI; + alpha_r = 180 -alpha ; + /*cout << " x1 = " << this->x1 << "\n"; + cout << " y1 = " << this->y1 + (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180) << "\n"; + cout << " x2 = " << this->x2 << "\n"; + cout << " y2 = " << this->y2 - (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180) << "\n"; + cout << " x3 = " << p->x << "\n"; + cout << " y3 = " << p->y << "\n";*/ + + + + + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) { // луч выше опт.оси пересечение под вспомагат.линии сверху + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "here_7\n"; + + } + + + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) { // луч ниже опт.оси пересечение за вспомагат.линии снизу + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "here_8\n"; + + } + } + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) + r->deg = alpha ; + else + r->deg = alpha_r; + } + } + else { // a = f + alpha = atan (line_tg) * 180 / PI; + alpha_r = alpha; + if (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0){ + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + } + + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) + r->deg = alpha ; + else + r->deg = alpha_r; + + } + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) { + if ((r->deg + this->deg - 360) > 0) + r->deg = (r->deg + this->deg - 360); + else + r->deg = r->deg + this->deg; + } + else { + + r->deg = r->deg + this->deg; + } + + r->x = p->x; + r->y = p->y; + } + const char *getID() const{ + return "Lens_wide"; + } + + ~Lens_wide () + { + cout << "Destructure of the Lens" << "\n"; + } +}; +//1) Плоское зеркало (XY – координаты; lenght – длина зеркала; deg_0 – угол поворота). +class PlainRefl : public Device { + public: + float deg, f; + +public: + PlainRefl( float x, float y, float l, float deg_0){ //deg from vertical 0 <= deg <= 90 against hour ; + x1 = x - (float)(l/2) * sin (GradToRad(deg_0)) ; + x2 = x + (float)(l/2) * sin (GradToRad(deg_0)); + y1 = y - (float)(l/2) * cos (GradToRad(deg_0)); + y2 = y + (float)(l/2) * cos (GradToRad(deg_0)); + deg = deg_0; + std::cout << "Create PlainRefl (" << x1 << ", " << y1 << "), (" << x2 << ", " << y2 << ") deg: " << deg << "\n"; + } + point *cross_point(RAY *r) const { + point *p = new point(); + if ((( this->x1 + this->x2 )/2 > r->x) && ((r->deg <= 90) || (r->deg > 270))){ + float det = this->y2 - this->y1 - tan (GradToRad(r->deg)) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 + - this->y1 * this->x2 + - (this->x1 - this->x2) * (r->y + tan (GradToRad(r->deg)) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (GradToRad(r->deg)) * r->x) + - (this->y2 * this->x1 - this->y1 * this->x2) * tan (GradToRad(r->deg)); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if ((( p->x >= x1 ) && ( p->x <= x2 ) && (x1 <= x2)) && (( p->y >= y1 ) && ( p->y <= y2 ) && (y1 <= y2))) return p; + if ((( p->x <= x1 ) && ( p->x >= x2 ) && (x1 >= x2)) && (( p->y >= y1 ) && ( p->y <= y2 ) && (y1 <= y2))) return p; + if ((( p->x >= x1 ) && ( p->x <= x2 ) && (x1 <= x2)) && (( p->y <= y1 ) && ( p->y >= y2 ) && (y1 >= y2))) return p; + if ((( p->x <= x1 ) && ( p->x >= x2 ) && (x1 >= x2)) && (( p->y <= y1 ) && ( p->y >= y2 ) && (y1 >= y2))) return p; + } + if ((( this->x1 + this->x2 )/2 < r->x) && ((r->deg >= 90) && (r->deg <= 270))) { + float det = this->y2 - this->y1 - tan (GradToRad(r->deg)) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (GradToRad(r->deg)) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (GradToRad(r->deg)) * r->x) + - (this->y2 * this->x1 - this->y1 * this->x2) * tan (GradToRad(r->deg)); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if ((( p->x >= x1 ) && ( p->x <= x2 ) && (x1 <= x2)) && (( p->y >= y1 ) && ( p->y <= y2 ) && (y1 <= y2))) return p; + if ((( p->x <= x1 ) && ( p->x >= x2 ) && (x1 >= x2)) && (( p->y >= y1 ) && ( p->y <= y2 ) && (y1 <= y2))) return p; + if ((( p->x >= x1 ) && ( p->x <= x2 ) && (x1 <= x2)) && (( p->y <= y1 ) && ( p->y >= y2 ) && (y1 >= y2))) return p; + if ((( p->x <= x1 ) && ( p->x >= x2 ) && (x1 >= x2)) && (( p->y <= y1 ) && ( p->y >= y2 ) && (y1 >= y2))) return p; + } + return NULL; + } + + void change_direction(RAY *r, point *p) const // Плоское зеркало + { + r->x = p->x; + r->y = p->y; + r->deg = (90 + this->deg) * 2 - r->deg; + + r->deg = r->Deg360(r->deg); + } + const char *getID() const { + return "PlainRefl"; + } + + ~PlainRefl () + { + cout << "Destructure of the PlainRefl" << "\n"; + } +}; + + +//2) Сферическое зеркало ( +// XY – координаты; +// lenght – длина зеркала; +// R – радиус кривизны; +// deg_1, deg_2 – углы, под которыми видны нижняя и верхняя границы зеркала). + +class SphereRefl : public Device { + public: + float deg1, deg2, R; + point *OptC; + SphereRefl( float x, float y, float R0, float deg_1, float deg_2) + { + OptC = new point(); //deg from vertical 0 <= deg <= 90 against hour ; + OptC->x = x; + OptC->y = y; + R = R0; // Радиус кривизны сферы + x1 = OptC->x - R * sin (GradToRad(deg_1)) ; + x2 = OptC->x - R * sin (GradToRad(deg_2)); + y1 = OptC->y - R * cos (GradToRad(deg_1)); + y2 = OptC->y - R * cos (GradToRad(deg_2)); + this->f = fabs(this->R); // фокусное расстояние + deg1 = deg_1; + deg2 = deg_2; + std::cout << "\nCreate SphereRefl x: " << x << " y: " << y << " R: " << R << " deg1:" << deg1 << " deg2: " << deg2 << "\n"; + std::cout << "X1:" << x1 << " Y1: " << y1 << " X2: " << x2 << " Y2: " << y2 << "\n"; + } + + point *cross_point(RAY *r) const { // точка пересечения сферы и луча + point *p = new point(); + + float xn, yn, k_ray, b_ray; + float xc, yc; + if (r->deg != 0) { + xn = r->y / tan (GradToRad(r->deg)) + r->x; + yn = r->x * tan (GradToRad(r->deg)) + r->y; + + k_ray = -yn/xn; // уравнение луча + b_ray = yn; + // пересечение луча и нормали, проходящей через центр сферы + xc = (OptC->y - OptC->x/k_ray - yn) / (k_ray + 1/k_ray); // k_ray = 0 ? + yc = -(xc + OptC->x)/ k_ray + OptC->y; + } else { + xc = OptC->x; + yc = r->y; + } + point pc; + pc.x = xc; + pc.y = yc; + point p_ray; + p_ray.x = r->x; + p_ray.y = r->y; + float l_ray_optc = LenVect(&p_ray, OptC); + float l_ray_pc = LenVect(&p_ray, &pc); + float l_d = sqrt(l_ray_optc * l_ray_optc - l_ray_pc * l_ray_pc); + float l_ray; + if (this->R >= l_d) { + l_ray = fabs(l_ray_pc - sqrt(R * R - l_d * l_d)); + p->x = r->x + l_ray * cos(GradToRad(r->deg)); + p->y = r->y - l_ray * sin(GradToRad(r->deg)); + + float delta = Sqr(p->x - OptC->x) + Sqr(p->y - OptC->y) - Sqr(R); + std::cout << "1) p->x: " << p->x << " p->y: " << p->y << " r->deg: " << r->deg << " delta:" << delta << "\n"; + // Проверка принадлежности сегменту + // Находим угол от вертикали + float deg_t; + if (fabs(delta) < 0.1) { + deg_t = RadToGrad(atan((OptC->x - p->x) / (OptC->y - p->y))); + deg_t = r->Deg360(deg_t); + + std::cout << "Sphere Point 1: x=" << p->x << " y=" << p->y << " Sphere Deg=" << deg_t << " deg1=" << deg1 << " deg2=" << deg2 << "\n"; + if (deg1 <= deg_t && deg_t <= deg2 ) return p; + } + + // Второй корень + l_ray = fabs(l_ray_pc + sqrt(R * R - l_d * l_d)); + p->x = r->x + l_ray * cos(GradToRad(r->deg)); + p->y = r->y - l_ray * sin(GradToRad(r->deg)); + + delta = Sqr(p->x - OptC->x) + Sqr(p->y - OptC->y) - Sqr(R); + std::cout << "2) p->x: " << p->x << " p->y: " << p->y << " r->deg: " << r->deg << " delta:" << delta << "\n"; + + if (fabs(delta) < 0.1) { + deg_t = RadToGrad(atan((OptC->x - p->x) / (OptC->y - p->y))); + deg_t = r->Deg360(deg_t); + + std::cout << "Sphere Point 2: x=" << p->x << " y=" << p->y << " Sphere Deg=" << deg_t << " deg1=" << deg1 << " deg2=" << deg2 << "\n"; + if (deg1 <= deg_t && deg_t <= deg2 ) return p; + } + } + + return NULL; + } + + void change_direction(RAY *r, point *p) const // Сферическое зеркало + { + std::cout << "\nPrev r->deg: " << r->deg << "\n"; + float a1 = p->x - r->x; + float b1 = r->y - p->y; + float a2 = p->x - OptC->x; + float b2 = OptC->y - p->y; + float deg_f = RadToGrad(atan((a1 * b2 - a2 * b1) / (a1 * a2 + b1 * b2))); // угол между лучом и нормалью к сфере + std::cout << "Deg ray-normal: " << deg_f << "\n"; + + + r->x = p->x; + r->y = p->y; + r->deg = r->deg + (180 - 2 * fabs(deg_f)); + r->deg = r->Deg360(r->deg); + std::cout << "New r->deg: " << r->deg << "\n"; + } + + const char *getID() const { + return "SphereRefl"; + } + + ~SphereRefl() + { + cout << "Destructure of the " << this->getID() << "\n"; + } +}; +class Gran { +public: + Prism *prism; + point *p1,*p2; + point *normal; + Gran(Prism *_prism, point *_p1, point *_p2) + { + prism = _prism; + p1 = new point; + p2 = new point; + p1->x = _p1->x; + p1->y = _p1->y; + p2->x = _p2->x; + p2->y = _p2->y; + normal = new point(); + normal->x = (p2->y - p1->y); + normal->y = (p1->x - p2->x); + } + + float CheckPointCosA (point *pp) const // точка pp внутри [p1,p2] + { + return Scalar(SubVect(p1, pp), SubVect(p2, pp)) / (LenVect(pp, p1) * LenVect(pp, p2)); + } + + bool CheckPoint (point *pp) const // точка pp внутри [p1,p2] + { + float cos_a = Scalar(SubVect(p1, pp), SubVect(p2, pp)) / (LenVect(pp, p1) * LenVect(pp, p2)); + if (cos_a < 0) return true; + return false; + } + + point *cross_point(RAY *r) const + { // точка пересечения грани призмы и луча + point *p = new point(); + float p_check = (r->x - p1->x) * (p2->y - p1->y) - (r->y - p1->y) * (p2->x - p1->x); // источник луча находится на грани призмы + if (fabs(p_check) < 0.1) return NULL; + float x1, y1, x2, y2, r1, r2, k_ray, b_ray; + float det, det1, det2; + float xp, yp; + + if (r->deg != 90 && r->deg != 270) { // tan 90 + x1 = p2->y - p1->y; + y1 = -(p2->x - p1->x); + r1 = p1->x * x1 + p1->y * y1; + + x2 = tan(GradToRad(r->deg)); + y2 = 1; + r2 = r->x * x2 + r->y; + + det = x1 * y2 - x2 * y1; // определитель - метод Крамера + det1 = r1 * y2 - r2 * y1; + det2 = x1 * r2 - x2 * r1; + + if (det == 0) return NULL; // прямые параллельны + p->x = det1 / det; + p->y = det2 / det; + } + else // луч параллелен оси y + { + x1 = p2->y - p1->y; + y1 = -(p2->x - p1->x); + r1 = p1->x * x1 + p1->y * y1; + + p->x = r->x; + p->y = (r1 - r->x * x1) / y1; + } + if (CheckPoint (p)) return p; + + + return NULL; + } +}; +//5) Призма (n - № объекта; XY– координаты; n – показатель преломления). +class Prism : public Device { +public: + int N; + float len, p, a , b, n_coeff; + vector gran_vect; + vector P; + float deg1, deg2; + point *OC; + + Prism(int _N, float _x1, float _y1, float _x2, float _y2, float _x3, float _y3, float _n_coeff) + { + N = _N; + n_coeff = _n_coeff; + Gran *gran_t; + point *p_t = new point; + p_t->x = _x1; + p_t->y = _y1; + P.push_back(p_t); + p_t = new point; + p_t->x = _x2; + p_t->y = _y2; + P.push_back(p_t); + p_t = new point; + p_t->x = _x3; + p_t->y = _y3; + P.push_back(p_t); + gran_t = new Gran((Prism *)this, P[0], P[1]); + gran_vect.push_back(gran_t); + gran_t = new Gran((Prism *)this, P[1], P[2]); + gran_vect.push_back(gran_t); + gran_t = new Gran((Prism *)this, P[2], P[0]); + gran_vect.push_back(gran_t); + + std::cout << "\n Create Prism N: " << N << "\n"; + for (int i = 0; i < P.size();i++) { + std::cout << "Point " << i << " x: " << P[i]->x << " y: " << P[i]->y << "\n"; + } + std::cout << "Coeff_N: " << n_coeff << "\n"; + } + + point *cross_point(RAY *r) const { // точка пересечения призмы и луча + point *p = NULL; + float len_min = 1e10; + + for (int i = 0; i < gran_vect.size(); i++) { + point *p_t = gran_vect[i]->cross_point(r); // проверка грани призмы и луча + if (p_t != NULL) { + if (r->CheckRayPoint(p_t)) { + point *pr = new point(); + pr->x = r->x; + pr->y = r->y; + float len_t = LenVect(p_t, pr); // минимальное расстояние до источника + if (len_min > len_t) { + len_min = len_t; + p = new point(); + p->x = p_t->x; + p->y = p_t->y; + } + } + } + } + if (p != NULL) { + std::cout << "Prism intersection \n"; + std::cout << "p->x: " << p->x << " p->y: " << p->y << " r->deg: " << r->deg << "\n"; + return p; + } + + return NULL; + } + + Gran *FindGran(point *pp) const + { + float cos_a = 0; + for (int i=0; i < gran_vect.size(); i++) { // нашли грань для точки p + cos_a = gran_vect[i]->CheckPointCosA(pp); + if (fabs(cos_a + 1) < 0.1) + { + return gran_vect[i]; + break; + } + } + return NULL; + } + + void change_direction(RAY *r, point *p) const // Призма + { + std::cout << "\nPrev r->deg: " << r->deg << "\n"; // исходный угол луча + std::cout << "Point_in: x: " << p->x << " y: " << p->y << " \n"; + std::cout << "Coeff n : " << n_coeff << " \n"; + float cos_a = 0; + Gran *gran = FindGran(p); + + point *r0 = new point(); + r0->x = r->x; + r0->y = r->y; + + cos_a = Scalar(SubVect(p, r0), gran->normal) / (LenVect(p, r0) * sqrt(Sqr(gran->normal->x) + Sqr(gran->normal->y))); + float deg_f = RadToGrad(acos(cos_a)); + std::cout << "Deg_in: " << deg_f << "\n"; + while (fabs(deg_f) > 90 ) { + deg_f = 180 - deg_f; + std::cout << "Deg_in: " << deg_f << " 180-a \n"; + } + float deg_r = RadToGrad(asin(sin(GradToRad(deg_f)) / n_coeff)); + std::cout << "Deg_r_in: " << deg_r << "\n"; + while (fabs(deg_r) > 90 ) { + deg_r = 180 - deg_r; + std::cout << "Deg_r_in: " << deg_r << " 180-a \n"; + } + + float deg_r_new = r->deg - deg_f + deg_r; + + std::cout << "Deg_ray_new " << deg_r_new << "\n"; + + RAY *ray_in = new RAY; + ray_in->set_ray_pos (p->x, p->y, ray_in->Deg360(deg_r_new)); + + point *p_new = cross_point(ray_in); // выходная точка + if (p_new != NULL) + { + Gran *gran_out = FindGran(p_new); + std::cout << "\n Point_out: x: " << p_new->x << " y: " << p_new->y << "\n"; + + cos_a = Scalar(SubVect(p_new, r0), gran_out->normal) / (LenVect(p_new, r0) * sqrt(Sqr(gran_out->normal->x) + Sqr(gran_out->normal->y))); + float deg_r_out = RadToGrad(acos(cos_a)); // угол между лучом и выходной нормалью + std::cout << " Deg_r_out: " << deg_r_out << "\n"; + while (fabs(deg_r_out) > 90 ) { + deg_r_out = 180 - deg_r_out; // 0..90 + std::cout << " Deg_r_out: " << deg_r_out << " 180-a \n"; + } + if (sin(GradToRad(deg_r_out)) * n_coeff > 1) { + std::cout << " sin(Deg_i_out) * n > 1: " << sin(GradToRad(deg_r_out)) * n_coeff << "\n"; + deg_r_out = 90 - deg_r_out; + } + float deg_i_out = RadToGrad(asin(sin(GradToRad(deg_r_out)) * n_coeff)); // sin(i_out) = sin(r_out) * n + std::cout << " Deg_i_out: " << deg_i_out << "\n"; + while (fabs(deg_i_out) > 90 ) { + deg_i_out = 180 - deg_i_out; // 0..90 + std::cout << " Deg_i_out: " << deg_i_out << " 180-a \n"; + } + + float deg_i_out_new = ray_in->deg - deg_i_out + deg_r_out; + std::cout << "Deg_i_out_new: " << deg_i_out_new << "\n"; + + r->deg = r->Deg360(deg_i_out_new); + r->x = p_new->x; + r->y = p_new->y; + } + r->x = p->x; + r->y = p->y; + r->deg = r->Deg360(r->deg); // угол луча в [0..360] + std::cout << "New r->deg: " << r->deg << "\n"; + } + + + const char *getID() const { + return "Prism"; + } + + ~Prism() + { + cout << "Destructure of the " << this->getID() << "\n"; + } +}; + + diff --git a/051315/first/first.cc b/051315/first/first.cc new file mode 100644 index 0000000..25629f4 --- /dev/null +++ b/051315/first/first.cc @@ -0,0 +1,60 @@ +#include +#include "device.h" +#include + + +float min_(float x1, float x2){ + if (x1 < x2) + return x1; + else return x2; +} +void print_(vector my_device){ + for (int i = 0; i < my_device.size(); i++){ + printf("%d:%f\n", i,min_(my_device[i]->x1, my_device[i]->x2)); + } + printf("\n"); +} +float sqr_(float x){ + return x*x; +} + +int first(vector d, RAY *r){ + int ret = -1; + float x = r->x; + float y = r->y; + float rast; + float min = 100000; + for (int i = 0; i < d.size(); i++){ + point *cross = NULL; + cross = d[i] -> cross_point(r); + if (cross != NULL){ + float xp = cross -> x; + float yp = cross -> y; + rast = sqrt(sqr_(x - xp)+sqr_(y - yp)); + } + else{ + rast = 1000000; + } + if (rast < min){ + min = rast; + ret = i; + } + } + return ret; +} + +int main(){ + vector my_device; + Device *d1 = new Lens(30, 0, 10, 5, 0); + Device *d2 = new Lens(10, 0, 10, 10, 0); + Device *d3 = new Lens(50, 0, 10, 20, 0); + my_device.push_back(d1); + my_device.push_back(d2); + my_device.push_back(d3); + RAY *r = new RAY(); + r->set_ray_pos(40, 0, 10); + int q = first(my_device, r); + printf("%d\n", q); +// sort_(my_device); + return 0; +} diff --git a/051315/first/first.h b/051315/first/first.h new file mode 100644 index 0000000..e1a0d14 --- /dev/null +++ b/051315/first/first.h @@ -0,0 +1,68 @@ +#include +#include "device.h" +#include + + +float min_(float x1, float x2){ + if (x1 < x2) + return x1; + else return x2; +} +void print_(vector my_device){ + for (int i = 0; i < my_device.size(); i++){ + printf("%d:%f\n", i,min_(my_device[i]->x1, my_device[i]->x2)); + } + printf("\n"); +} +float sqr_(float x){ + return x*x; +} + +int first(vector d, RAY *r){ + int ret = -1; + float x = r->x; + float y = r->y; + float rast; + float min = 100000; + for (int i = 0; i < d.size(); i++){ + point *cross = NULL; + cross = d[i] -> cross_point(r); + if (cross != NULL){ + float xp = cross -> x; + float yp = cross -> y; + rast = sqrt(sqr_(x - xp)+sqr_(y - yp)); + } + else{ + rast = 1000000; + } + if (rast < min){ + min = rast; + ret = i; + } + } + return ret; +} +int first_s(vector d, RAY *r){ + int ret = -1; + float x = r->x; + float y = r->y; + float rast; + float min = 100000; + for (int i = 0; i < d.size(); i++){ + point *cross = NULL; + cross = d[i] -> cross_point(r); + if (cross != NULL){ + float xp = cross -> x; + float yp = cross -> y; + rast = sqrt(sqr_(x - xp)+sqr_(y - yp)); + } + else{ + rast = 1000000; + } + if (rast < min){ + min = rast; + ret = i; + } + } + return ret; +} diff --git a/051315/first/main.cc b/051315/first/main.cc new file mode 100644 index 0000000..ef4e9a1 --- /dev/null +++ b/051315/first/main.cc @@ -0,0 +1,289 @@ +#include +#include +#include +#include +#include +#include +#include +#include +//#include "ray.h" //was included in "device.h" +#include +#include +#include "first.h" + + +int h; + +void handler(int nsig){ + if(nsig==SIGINT){ + close(h); + printf("\nShutting down\n"); + exit(0); + } +} +void *func(void *arg); +int main() +{ + (void)signal(SIGINT, handler); + h = socket(PF_INET, SOCK_STREAM, 0); + if (h < 0){ + perror("Creating socket"); + } + const int PORT = 5678; + struct sockaddr_in local; + local.sin_family = PF_INET; + local.sin_port = htons(PORT); + if(bind(h, (sockaddr *)&local, sizeof(local)) < 0){ + perror("Binding"); + close(h); + return 1; + } + if (listen(h, 10) < 0){ + perror("Listening"); + close(h); + return 2; + } + while(1) + { + struct sockaddr_in remote; + unsigned remoteLen=sizeof(remote); + int cs; + if((cs=accept(h, (sockaddr *)&remote, &remoteLen))<0){ + perror("Accepting"); + } + pthread_t thread; + if(pthread_create(&thread, NULL, func, &cs)<0) + { + perror("Thread: "); + } + } + close(h); +} + +void *func(void* arg) +{ +int rd; +char buf[512]; +int cs=*(int *)arg; + char buf_[32]; + + vector my_device; + vector my_screen; + SCREEN *scr1 = new SCREEN(5000, 5000, 5000, 5010); + SCREEN *scr2 = new SCREEN(5000, -5000, 5000, -5010); + SCREEN *scr3 = new SCREEN(-5000, 5000, -5000, -5010); + SCREEN *scr4 = new SCREEN(-5000, -5000, -5000, -5010); + my_screen.push_back(scr1); + my_screen.push_back(scr2); + my_screen.push_back(scr3); + my_screen.push_back(scr4); + + vector my_laser; + vector my_source; + if(send(cs, "1", 1, MSG_NOSIGNAL)==-1) + { + perror("Can't send:"); + return NULL; + } + while((rd=recv(cs, buf, sizeof(buf), 0))>0){ + buf[rd]=0; + printf("%s\n", buf); + int check; + + if ((buf[1]>='0')&&(buf[1]<='9')){ + int check_10 = buf[0] - '0'; + check_10 = check_10 * 10; + int check_1 = buf[1] - '0'; + check = check_10 + check_1; + printf("check = %d\n", check); + } + else{ + check = buf[0] - '0'; + } + switch(check){ + case 0: //source + { + float a1,x,y; + sscanf(buf, "%f %f %f", &a1, &x, &y); + SOURCE* d=new SOURCE(x,y); + my_source.push_back(d); + printf("New source was created\n"); + break; + } + case 1: //screen + { + float a1, x1, y1, x2, y2; + sscanf(buf, "%f %f %f %f %f", &a1, &x1, &y1, &x2, &y2); + SCREEN *my_scr = new SCREEN(x1, y1, x2, y2); + my_screen.push_back(my_scr); + printf("Screen was created\n"); + break; + } + case 2: //lens f>0 + { + float a1, x, y, l, deg, f; + sscanf(buf, "%f %f %f %f %f %f",&a1,&x, &y, &l, °, &f); + Device *d = new Lens(x, y, l, deg, f); + my_device.push_back(d); + printf("New lens f>0 was created\n"); + break; + } + case 3: //mirror == PlainRefl + { + float a1, x, y, l, deg; + sscanf(buf, "%f %f %f %f %f",&a1,&x, &y, &l, °); + Device *d = new PlainRefl(x, y, l, deg); + my_device.push_back(d); + printf("New mirror was created\n"); + break; + } + + case 4: //ploskoparallell plastinka == disc + { + float a1, x, y, len, wid, deg, n; + sscanf(buf, "%f %f %f %f %f %f %f",&a1,&x, &y, &len, &wid, °, &n); + Device *d = new Disc(x, y, len, wid, deg, n); + my_device.push_back(d); + printf("New ploskoparallell plastinka was created\n"); + break; + } + case 5: //Laser + { + float a1, x, y, deg; + sscanf(buf, "%f %f %f %f", &a1,&x, &y, °); + my_laser.push_back(new LASER(x,y,deg)); + printf("New laser was created\n"); + break; + } + case 6: //triangle prism + { + float a1, x1, y1, x2, y2, x3, y3, n; + sscanf(buf, "%f %f %f %f %f %f %f %f",&a1,&x1, &y1, &x2, &y2, &x3, &y3, &n); + int num = 1; + Device *d = new Prism(num,x1, y1, x2, y2, x3, y3, n); + my_device.push_back(d); + printf("New triangle prism was created\n"); + break; + } + + case 7: //sphere mirror + { + float a1, x, y, r0, deg_1, deg_2; + sscanf(buf, "%f %f %f %f %f %f",&a1, &x, &y, °_1, °_2, &r0); + Device *d = new SphereRefl(x, y, r0, deg_1-90, deg_2-90); + my_device.push_back(d); + printf("New sphere mirror was created\n"); + break; + } + case 8: //wide length + { + float a1, x, y, l, deg, r1, r2, n, de; + sscanf(buf, "%f %f %f %f %f %f %f %f %f",&a1, &x, &y, &r1, &r2, °, &n, &l, &de); + Device *d = new Lens_wide(x, y, l, deg, r1, r2, n, de); + my_device.push_back(d); + printf("New triangle prism was created\n"); + break; + } + } + + fflush(stdout); + if (strcmp(buf, "FINISH\0")!=0){ + buf[0]=0; + if(send(cs, "1", 1, MSG_NOSIGNAL)==-1) + { + perror("Can't send:"); + return NULL; + } + } + else{ + break; + } + } + + + + point *cross = NULL; + int k = 0; //номер девайса + bool q = false; //true, если пересечения есть + char temp[1]; + vectormy_laser_ray; + for(int i=0; irays_create()); + } +//RAY *my_laser_ray=my_laser->rays_create(); + for(int i=0; irays_create(); + for(int j=0; jcross_point(my_laser_ray[I]); + sprintf(buf_, "%f %f %f %f %c", my_laser_ray[I]->x, my_laser_ray[I]->y, cross->x, cross->y, '\0');//new dot + if(send(cs, buf_, strlen(buf_)+1, MSG_NOSIGNAL)==-1){ + perror("Can't send:"); + return NULL; + } + recv(cs, temp, 1, 0); + float tx=cross->x; + float ty=cross->y; + my_device[num]->change_direction(my_laser_ray[I], cross); + if(my_device[num]->getID()==4){ + sprintf(buf_, "%f %f %f %f %c", tx, ty, my_laser_ray[I]->x, my_laser_ray[I]->y, 0); + if(send(cs, buf_, strlen(buf_)+1, MSG_NOSIGNAL)==-1){ + perror("Can't send:"); + return NULL; + } + recv(cs, temp, 1, 0); + } + } + else{ + break; + } + } + //cross screen + int s_num = first_s(my_screen, my_laser_ray[I]); + if (s_num == -1){ + //find граница, куда дойдет луч + float retx, rety; + retx = my_laser_ray[I]->x + 2000 * cos(GradToRad(my_laser_ray[I]->deg)); + rety = my_laser_ray[I]->y + 2000 * sin(GradToRad(my_laser_ray[I]->deg)); + sprintf(buf_, "%f %f %f %f %c", my_laser_ray[I]->x, my_laser_ray[I]->y, retx, rety, '\0'); + if(send(cs, buf_, strlen(buf_)+1, MSG_NOSIGNAL)==-1) + { + perror("Can't send:"); + return NULL; + } + recv(cs, temp, 1, 0); + + } + else{ + cross = my_screen[s_num]->cross_point(my_laser_ray[I]); + sprintf(buf_, "%f %f %f %f %c", my_laser_ray[I]->x, my_laser_ray[I]->y, cross->x, cross->y, '\0'); + if(send(cs, buf_, strlen(buf_)+1, MSG_NOSIGNAL)==-1) + { + perror("Can't send:"); + return NULL; + } + recv(cs, temp, 1, 0); + } +} + if(send(cs, "FINISH\0", 7, MSG_NOSIGNAL)==-1) + { + perror("Can't send:"); + return NULL; + } + while(recv(cs, buf_, sizeof(buf_)+1, 0)>0); + close(cs); + return NULL; + } diff --git a/051315/first/ray.h b/051315/first/ray.h new file mode 100644 index 0000000..1840228 --- /dev/null +++ b/051315/first/ray.h @@ -0,0 +1,388 @@ +#include +#include +#include "math.h" +#include + +using namespace std; + +#define PI 3.14159265 +#define NUMBER 10 // HERE YOU CAN CHANGE NUMBER OF CREATED RAYS {360 : (NUMBBER - 1)} + + +struct point { + float x ; + float y ; +}; +inline float GradToRad(float deg) { return (float)(deg*PI/180.0);}; +inline float RadToGrad(float arg) { return (float)(arg*180.0/PI);}; +inline float Sqr(float x) {return x*x;} + +int orient_ (float x1, float y1, float x2, float y2, float x3, float y3, float this_deg) {// x1,y1; x2,y2 - line x3,y3 - point + // cout << "x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 << " x3 = " << x1 << " y3 = " << y3 << "\n"; + if (this_deg == 0){ + if (y3 <= y2){ + + // cout << "trying 1 \n"; + return 1; + } + else{ + // cout << "trying -1 \n"; + return -1; + } + } + else { + if (((x3 -x1) * (y2 - y1) - (y3 -y1) * (x2 - x1)) >= 0) + return 1; + else + return -1; + } + +} + +/* +class RAY{ +public: + float x, y; + float deg; + void set_ray_pos(float x_0, float y_0, float deg_0){ + x = x_0; + y = y_0; + deg = deg_0; + } + void show_ray_pos(int i){ + printf("%d: x = %f, y = %f, deg = %f\n", i, x, y, deg); + } + RAY(float x_0, float y_0, float deg_0){ + x = x_0; + y = y_0; + deg = deg_0; + } + ~RAY (){ + printf("Ray was destructed\n"); + } +};*/ + +/*class RAY +{ +public: + float x, y; + float deg; + +public: + void set_ray_pos (float x_0, float y_0, float deg_0) + { x = x_0; y = y_0; deg = deg_0;} + void show_ray_pos(int i) + { cout <y - pp->y), (pp->x - this->x))); + r_deg_delta = Deg360(r_deg_delta); + float r_deg = this->deg; + if (r_deg == 360) r_deg = 0; + if (fabs(r_deg - r_deg_delta) < 1) return true; + return false; + } + + void show_ray_pos(int i) + { cout << "Ray(" << i << "):" << " x = " << x << " y = " << y << " deg = " << deg << "\n";} + + float Deg360(float deg1) const + { // угол в диапазоне [0..360] + do { + if (deg1 < 0) deg1 += 360; + if (deg1 >360) deg1 -= 360; + } + while (deg1 < 0 || deg1 > 360); + return deg1; + } + + ~RAY () + { + cout << "Destructure of the ray (" << deg << ") x: " << x << " y: " << y <<"\n"; + } +}; + + +class SCREEN +{ + public : + float x1, x2, y1, y2,deg; +public: + SCREEN (float x, float y, float l_0, float deg_0) { + x1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180); + x2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180); + y1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180); + y2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180); + deg = deg_0; + } + void screen_pos() + { cout << " x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 <<"\n";} + + point * cross_point (RAY * r) { + point * p = new point (); + if ((orient_ (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))){ + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + if ((orient_ (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + cout << "p->x = " << p->x << "\n" ; + p->y = (float)det_2/det; + cout << "p->x = " << p->x << "\n" ; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } return NULL; + } + + ~SCREEN () + { + cout << "Destructure of the screen" << "\n"; + } +}; + +/*class SCREEN +{ +public : + float x1, x2, y1, y2; + SCREEN (float x1_0, float y1_0, float x2_0, float y2_0 ) : x1 (x1_0), y1 (y1_0), x2 (x2_0), y2 (y2_0) + { } + void screen_pos(){ + cout << " x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 <<"\n"; + } + + point * cross_point (RAY * r){ + point * p = new point (); + if ((( this->x1 + this->x2 )/2 > r->x) && ((r->deg <= 90) || (r->deg >= 270))) { + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + if ((( this->x1 + this->x2 )/2 > r->x) && ((r->deg <= 90) || (r->deg >= 270))) { + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + + return NULL; + } + + ~SCREEN (){ + cout << "Destructure of the screen" << "\n"; + } +}; +*/ +class SOURCE +{ +public: + float x, y; + +public: + SOURCE (float x_0, float y_0) : + x(x_0), y(y_0) + { } + void source_pos() + { cout << " x = " << x << " y = " << y << "\n";} + RAY** rays_create () + { const int num_of_rays = NUMBER; + float deg_step = (float)360 / (num_of_rays - 1); + RAY ** ray = new RAY *[num_of_rays]; + float deg_i = 0; + for (int i = 0; i < num_of_rays; i++) { + ray[i]=new RAY(); + ray[i]->set_ray_pos (x, y, deg_i); + ray[i]->show_ray_pos(i); + deg_i = deg_i + deg_step; + } + return ray; + } + ~SOURCE () + { + cout << "Destructure of the source" << "\n"; + } +}; + + +/* +class Laser{ +public: + RAY *ray; + Laser(float x, float y, float deg){ + ray = new RAY(x, y, deg); + } + ~Laser(){ + printf("Laser destructed\n"); + } +}; +*/ + + + +/*class SCREEN +{ + public : + float x1, x2, y1, y2,deg; +public: + SCREEN (float x, float y, float l_0, float deg_0) { + x1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180); + x2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180); + y1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180); + y2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180); + deg = deg_0; + } + void screen_pos() + { cout << " x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 <<"\n";} + + point * cross_point (RAY * r) { + point * p = new point (); + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))){ + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + cout << "p->x = " << p->x << "\n" ; + p->y = (float)det_2/det; + cout << "p->x = " << p->x << "\n" ; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } return NULL; + } + + ~SCREEN () + { + cout << "Destructure of the screen" << "\n"; + } +}; + +class LASER +{ +private: + float x, y, deg; + +public: + LASER (float x_0, float y_0, float deg_0) : + x(x_0), y(y_0), deg (deg_0) + { } + void laser_pos() + { cout << " x = " << x << " y = " << y << "\n";} + RAY * rays_create () + { + RAY * ray = new RAY (); + ray->set_ray_pos (x, y, deg); + ray->show_ray_pos(0); + return ray; + } + ~LASER () + { + cout << "Destructure of the source" << "\n"; + } +}; +class SCREEN +{ + public : + float x1, x2, y1, y2,deg; +public: + SCREEN (float x, float y, float l_0, float deg_0) { + x1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180); + x2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180); + y1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180); + y2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180); + deg = deg_0; + } + void screen_pos() + { cout << " x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 <<"\n";} + + point * cross_point (RAY * r) { + point * p = new point (); + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))){ + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + cout << "p->x = " << p->x << "\n" ; + p->y = (float)det_2/det; + cout << "p->x = " << p->x << "\n" ; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } return NULL; + } + + ~SCREEN () + { + cout << "Destructure of the screen" << "\n"; + } +}; +*/ +class LASER +{ +private: + float x, y, deg; + +public: + LASER (float x_0, float y_0, float deg_0) : + x(x_0), y(y_0), deg (deg_0) + { } + void laser_pos() + { cout << " x = " << x << " y = " << y << "\n";} + RAY * rays_create () + { + RAY * ray = new RAY (); + ray->set_ray_pos (x, y, deg); + ray->show_ray_pos(0); + return ray; + } + ~LASER () + { + cout << "Destructure of the source" << "\n"; + } +}; + From 2eff67c0e94de2528738c5d0a9c7214e16334a3d Mon Sep 17 00:00:00 2001 From: AntonGerasimov Date: Sun, 17 May 2015 20:17:53 +0300 Subject: [PATCH 02/19] Delete this 4 screens --- 051315/first/main.cc | 9 --------- 1 file changed, 9 deletions(-) diff --git a/051315/first/main.cc b/051315/first/main.cc index ef4e9a1..1f89138 100644 --- a/051315/first/main.cc +++ b/051315/first/main.cc @@ -69,15 +69,6 @@ int cs=*(int *)arg; vector my_device; vector my_screen; - SCREEN *scr1 = new SCREEN(5000, 5000, 5000, 5010); - SCREEN *scr2 = new SCREEN(5000, -5000, 5000, -5010); - SCREEN *scr3 = new SCREEN(-5000, 5000, -5000, -5010); - SCREEN *scr4 = new SCREEN(-5000, -5000, -5000, -5010); - my_screen.push_back(scr1); - my_screen.push_back(scr2); - my_screen.push_back(scr3); - my_screen.push_back(scr4); - vector my_laser; vector my_source; if(send(cs, "1", 1, MSG_NOSIGNAL)==-1) From ab1bc874fded82e4b63485d5c8b3f58bb48fbf12 Mon Sep 17 00:00:00 2001 From: AntonGerasimov Date: Sun, 17 May 2015 20:19:46 +0300 Subject: [PATCH 03/19] change + to - in y --- 051315/first/main.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/051315/first/main.cc b/051315/first/main.cc index 1f89138..a3290a0 100644 --- a/051315/first/main.cc +++ b/051315/first/main.cc @@ -248,7 +248,7 @@ for(int I=0; Ix + 2000 * cos(GradToRad(my_laser_ray[I]->deg)); - rety = my_laser_ray[I]->y + 2000 * sin(GradToRad(my_laser_ray[I]->deg)); + rety = my_laser_ray[I]->y - 2000 * sin(GradToRad(my_laser_ray[I]->deg)); sprintf(buf_, "%f %f %f %f %c", my_laser_ray[I]->x, my_laser_ray[I]->y, retx, rety, '\0'); if(send(cs, buf_, strlen(buf_)+1, MSG_NOSIGNAL)==-1) { From d65e92928b6d13bec2be193bc13ac47b38f961ff Mon Sep 17 00:00:00 2001 From: AntonGerasimov Date: Sun, 17 May 2015 20:21:55 +0300 Subject: [PATCH 04/19] Add after 1116 --- 051315/first/device.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/051315/first/device.h b/051315/first/device.h index 75cf91d..f0364ec 100644 --- a/051315/first/device.h +++ b/051315/first/device.h @@ -1114,7 +1114,9 @@ class SphereRefl : public Device { float deg_t; if (fabs(delta) < 0.1) { deg_t = RadToGrad(atan((OptC->x - p->x) / (OptC->y - p->y))); - deg_t = r->Deg360(deg_t); + if (OptC->x - p->x < 0) deg_t += 180{ + deg_t = r->Deg360(deg_t); + } std::cout << "Sphere Point 1: x=" << p->x << " y=" << p->y << " Sphere Deg=" << deg_t << " deg1=" << deg1 << " deg2=" << deg2 << "\n"; if (deg1 <= deg_t && deg_t <= deg2 ) return p; From 442a477be3115c11d88f48c3ab9848b9d0cbcfb1 Mon Sep 17 00:00:00 2001 From: AntonGerasimov Date: Sun, 17 May 2015 20:25:51 +0300 Subject: [PATCH 05/19] Add 1116-1117 --- 051315/first/device.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/051315/first/device.h b/051315/first/device.h index f0364ec..727f243 100644 --- a/051315/first/device.h +++ b/051315/first/device.h @@ -1114,9 +1114,8 @@ class SphereRefl : public Device { float deg_t; if (fabs(delta) < 0.1) { deg_t = RadToGrad(atan((OptC->x - p->x) / (OptC->y - p->y))); - if (OptC->x - p->x < 0) deg_t += 180{ - deg_t = r->Deg360(deg_t); - } + if (OptC->x - p->x < 0) deg_t += 180; + deg_t = r->Deg360(deg_t); std::cout << "Sphere Point 1: x=" << p->x << " y=" << p->y << " Sphere Deg=" << deg_t << " deg1=" << deg1 << " deg2=" << deg2 << "\n"; if (deg1 <= deg_t && deg_t <= deg2 ) return p; From 9da1208f25c6fade679667c857b40fee96dd1710 Mon Sep 17 00:00:00 2001 From: AntonGerasimov Date: Sun, 17 May 2015 20:56:17 +0300 Subject: [PATCH 06/19] Update ray.h --- 051315/ray.h | 1 - 1 file changed, 1 deletion(-) diff --git a/051315/ray.h b/051315/ray.h index 765fad1..fd89441 100644 --- a/051315/ray.h +++ b/051315/ray.h @@ -8,7 +8,6 @@ using namespace std; #define PI 3.14159265 #define NUMBER 37 // HERE YOU CAN CHANGE NUMBER OF CREATED RAYS {360 : (NUMBBER - 1)} - struct point { float x ; float y ; From 740579d1a1b98d23f06a3ec90f22db48a8d85784 Mon Sep 17 00:00:00 2001 From: AntonGerasimov Date: Sun, 17 May 2015 20:58:59 +0300 Subject: [PATCH 07/19] Check main --- 051315/main/device.h | 1409 ++++++++++++++++++++++++++++++++++++++++++ 051315/main/first.h | 71 +++ 051315/main/main.cc | 280 +++++++++ 051315/main/ray.h | 388 ++++++++++++ 4 files changed, 2148 insertions(+) create mode 100644 051315/main/device.h create mode 100644 051315/main/first.h create mode 100644 051315/main/main.cc create mode 100644 051315/main/ray.h diff --git a/051315/main/device.h b/051315/main/device.h new file mode 100644 index 0000000..727f243 --- /dev/null +++ b/051315/main/device.h @@ -0,0 +1,1409 @@ +#include +#include +#include "math.h" +#include "ray.h" +using namespace std; + +#define PI 3.14159265 +#define NUMBER 10 // HERE YOU CAN CHANGE NUMBER OF CREATED RAYS {360 : (NUMBBER - 1)} + + +float LenVect(point *p1, point *p2) { + return sqrt(Sqr(p2->x - p1->x) + Sqr(p2->y - p1->y)); +} + +float Scalar(point *p1, point *p2) { + return p1->x * p2->x + p1->y * p2->y; +} + +point *SumVect(point *p1, point *p2) { + point *sum_p = new point; + sum_p->x = p1->x + p2->x; + sum_p->y = p1->y + p2->y; + return sum_p; +} + +point *SubVect(point *p1, point *p2) { + point *sub_p = new point; + sub_p->x = p2->x - p1->x; + sub_p->y = p2->y - p1->y; + return sub_p; +} +class Prism; + +int orient (float x1, float y1, float x2, float y2, float x3, float y3, float this_deg) {// x1,y1; x2,y2 - line x3,y3 - point + // cout << "x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 << " x3 = " << x1 << " y3 = " << y3 << "\n"; + if (this_deg == 0){ + if (y3 <= y2){ + + // cout << "trying 1 \n"; + return 1; + } + else{ + // cout << "trying -1 \n"; + return -1; + } + } + else { + if (((x3 -x1) * (y2 - y1) - (y3 -y1) * (x2 - x1)) >= 0) + return 1; + else + return -1; + } +} + +class Device{ +public: + float x1, x2, y1, y2, deg, f ; // y1 - up y2 - down x1 - left x2 - right + virtual void change_direction(RAY * r, point * p) const = 0; + //virtual const char *getID() const = 0; + virtual int getID() + { + return -1; + } + virtual point * cross_point (RAY * r) const = 0; +}; +class Disc : public Device{ // n > 1 !!! +public: + float w,l,n, x3,y3, x4, y4, deg; // w - width + +public: + Disc ( float x, float y, float l_0, float w_0, float deg_0, float n_0){ + float x_1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180) - (float) w_0 / 2 * cos (deg_0 * PI / 180) ; + float x_2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180) - (float) w_0 / 2 * cos (deg_0 * PI / 180) ; + float y_1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180) + (float) w_0 / 2 * sin (deg_0 * PI / 180) ; + float y_2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180) + (float) w_0 / 2 * sin (deg_0 * PI / 180) ; + + float x_3 = x_1 + (float)(w_0) * cos (deg_0 * PI / 180); + float x_4 = x_2 + (float)(w_0) * cos (deg_0 * PI / 180); + float y_3 = y_1 - (float)(w_0) * sin (deg_0 * PI / 180); + float y_4 = y_2 - (float)(w_0) * sin (deg_0 * PI / 180); + + deg = deg_0; + w = w_0; + l = l_0; + n = n_0; + + x1 = x_1 * cos (deg * PI / 180) - y_1 * sin (deg * PI / 180); + x2 = x_2 * cos (deg * PI / 180) - y_2 * sin (deg * PI / 180); + x3 = x_3 * cos (deg * PI / 180) - y_3 * sin (deg * PI / 180); + x4 = x_4 * cos (deg * PI / 180) - y_4 * sin (deg * PI / 180); + + + y1 = x_1 * sin (deg * PI / 180) + y_1 * cos (deg * PI / 180); + y2 = x_2 * sin (deg * PI / 180) + y_2 * cos (deg * PI / 180); + y3 = x_3 * sin (deg * PI / 180) + y_3 * cos (deg * PI / 180); + y4 = x_4 * sin (deg * PI / 180) + y_4 * cos (deg * PI / 180); + + + cout << "x_1 = " << x_1 << " x_2 = " << x_2 << " x_3 = " << x_3 << " x_4 = " << x_4 << " y_1 = " << y_1 << " y_2 = " << y_2 << " y_3 = " << y_3 << " y_4 = " << y_4 << "\n"; + + cout << "x1 = " << x1 << " x2 = " << x2 << " x3 = " << x3 << " x4 = " << x4 << " y1 = " << y1 << " y2 = " << y2 << " y3 = " << y3 << " y4 = " << y4 << "\n"; + + + + } + int getID() + { + return 4; + } + point * cross_point (RAY * r) const { + + point * p = new point (); + float p_x, p_y; + float r_x, r_y; + r_x = r->x; + r_y = r->y; + r->x = r_x * cos (this->deg * PI / 180) - r_y * sin (this->deg * PI / 180); + r->y = r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + if (r->deg >= this->deg) + r->deg = r->deg - this->deg; + else + r->deg = 360 + r->deg -this->deg; + cout << "r->x = " << r->x << " r->y = " << r->y << " r->deg = " << r->deg << "\n"; + + + + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))) { + cout << "h_0\n"; + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + cout << "det = " << det << "\n"; + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + cout << "det_1 = " << det_1 << "\n"; + + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + cout << "det_2 = " << det_2 << "\n"; + + + p->x = (float)det_1/det; + p->y = (float)det_2/det; + + cout << "p->x = " << p->x << " p->y = " << p->y << "\n"; + cout << "x1 = " << x1 << " x2 = " << x2 << " y1 = " << y1 << " y2 = " << y2 << "\n"; + + + if ( ( p->y >= y1 ) && ( p->y <= y2 )) + { + p_x = p->x; + p_y = p->y; + p->x = p_x * cos (this->deg * PI / 180) + p_y * sin (this->deg * PI / 180); + p->y = - p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); + r_x = r->x; + r_y = r->y; + r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); + r->y = -r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + return p; + } + } + if ((orient (this->x3,this->y3,this->x4,this->y4,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x3 - this->x4); + float det_1 = this->y2 * this->x3 - this->y1 * this->x4 - (this->x3 - this->x4) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x3 - this->y1 * this->x4) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->y >= y1 ) && ( p->y <= y2 )) + { + p_x = p->x; + p_y = p->y; + p->x = p_x * cos (this->deg * PI / 180) + p_y * sin (this->deg * PI / 180); + p->y = - p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); + r_x = r->x; + r_y = r->y; + r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); + r->y = -r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + return p; + } + } + + if ((orient (this->x1,this->y1,this->x3,this->y3,r->x,r->y,1) > 0) && ((r->deg >= 180) && (r->deg <= 360))) { + //cout << "this->y1 = "<< this->y1 << "\n"; + float det = this->x3 - this->x1 - (float) cos (r->deg * PI / 180) / sin (r->deg * PI / 180) * (this->y1 - this->y3); + //cout << "det = " << det << "\n"; + float det_1 = this->x3 * this->y1 - this->x1 * this->y3 - (this->y1 - this->y3) * (r->x + (float) cos (r->deg * PI / 180) / sin (r->deg * PI / 180) * r->y); + //cout << "det_1 = " << det_1 << "\n"; + float det_2 = (this->x3 - this->x1) * (r->x + (float) cos (r->deg * PI / 180) / sin (r->deg * PI / 180) * r->y) - (this->x3 * this->y1 - this->x1 * this->y3) * (float) cos (r->deg * PI / 180) / sin (r->deg * PI / 180); + cout << "det_2 = " << det_2 << "\n"; + p->x = (float)det_1/det; + p->y = (float)det_2/det; + float t = p->y; + p->y = p->x; + p->x = t; + //cout << "p->x = " << p->x << "\n"; + //cout << "p->y = " << p->y << "\n"; + if (( p->x >= x1 ) && ( p->x <= x3 )) + { + p_x = p->x; + p_y = p->y; + p->x = p_x * cos (this->deg * PI / 180) + p_y * sin (this->deg * PI / 180); + p->y = - p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); + r_x = r->x; + r_y = r->y; + r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); + r->y = -r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + return p; + } + } + + if ((orient (this->x2,this->y2,this->x4,this->y4,r->x,r->y,1) < 0) && ((r->deg >= 0) && (r->deg <= 180))) { + + float det = this->x4 - this->x2 - (float) cos (r->deg * PI / 180) / sin (r->deg * PI / 180) * (this->y2 - this->y4); + float det_1 = this->x4 * this->y2 - this->x2 * this->y4 - (this->y2 - this->y4) * (r->x + (float) cos (r->deg * PI / 180) / sin (r->deg * PI / 180) * r->y); + float det_2 = (this->x4 - this->x2) * (r->x + (float) cos (r->deg * PI / 180) / sin (r->deg * PI / 180) * r->y) - (this->x4 * this->y2 - this->x2 * this->y4) * (float) cos (r->deg * PI / 180) / sin (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + float t = p->y; + p->y = p->x; + p->x = t; + if (( p->x >= x2 ) && ( p->x <= x4 )) + { + p_x = p->x; + p_y = p->y; + p->x = p_x * cos (this->deg * PI / 180) + p_y * sin (this->deg * PI / 180); + p->y = - p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); + r_x = r->x; + r_y = r->y; + r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); + r->y = -r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + return p; + } + } + + + + return NULL; + } + + + void change_direction(RAY * r, point * p ) const { + float p_x, p_y, r_x,r_y; + p_x = p->x; + p_y = p->y; + p->x = p_x * cos (this->deg * PI / 180) - p_y * sin (this->deg * PI / 180); + p->y = p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); + r_x = r->x; + r_y = r->y; + r->x = r_x * cos (this->deg * PI / 180) - r_y * sin (this->deg * PI / 180); + r->y = r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + + point * new_p = p; + float deg = 0; + + if ((p->y <= this->y2) && (p->y >= this->y1)) { + float beta = asin (fabs (sin (r->deg * PI / 180) / this->n) )* 180 / PI; + if ((((fabs((this->y1 - p->y)) * tan ((90-beta) * PI / 180)) >= this->w) && (r->y >= p->y)) || (((fabs((this->y2 - p->y)) * tan ((90-beta) * PI / 180)) >= this->w) && (r->y <= p->y))) { + if (r->x < p->x){ + if (r->y >= p->y) { + new_p->x = p->x + this->w; + new_p->y = p->y - this->w * tan ( beta * PI / 180); + } + else { + beta = fabs (beta); + new_p->x = p->x + this->w; + new_p->y = p->y + this->w * tan ( beta * PI / 180); + } + } + + else { + if (r->y >= p->y) { + new_p->x = p->x - this->w; + new_p->y = p->y - this->w * tan ( beta * PI / 180); + } + else { + beta = fabs (beta); + new_p->x = p->x - this->w; + new_p->y = p->y + this->w * tan ( beta * PI / 180); + } + } + deg = r->deg; + } + else { + //cout << "yes \n" ; + if (r->x < p->x){ + if (r->y >= p->y) { + new_p->x = p->x + fabs((this->y1 - p->y)) * tan ((90-beta) * PI / 180); + new_p->y = this->y1; + if ((cos (beta * PI / 180) * this->n) <= 1) + deg = 90 - asin (cos (beta * PI / 180) * this->n) * 180 / PI; + else + deg = 0; + } + else { + beta = fabs (beta); + new_p->x = p->x + fabs((this->y2 - p->y)) * tan ((90-beta) * PI / 180); + new_p->y = this->y2; + if ((cos (beta * PI / 180) * this->n) <= 1) + deg = 270 + asin (cos (beta * PI / 180) * this->n) * 180 / PI; + else + deg = 360; + } + } + + else { + if (r->y >= p->y) { + new_p->x = p->x - fabs((this->y1 - p->y)) * tan ((90-beta) * PI / 180); + new_p->y = this->y1; + if ((cos (beta * PI / 180) * this->n) <= 1) + deg = 90 + asin (cos (beta * PI / 180) * this->n) * 180 / PI; + else + deg = 180; + } + else { + beta = fabs (beta); + new_p->x = p->x - fabs((this->y2 - p->y)) * tan ((90-beta) * PI / 180); + new_p->y = this->y2; + if ((cos (beta * PI / 180) * this->n) <= 1) + deg = 270 - asin (cos (beta * PI / 180) * this->n) * 180 / PI; + else + deg = 180; + } + } + } + } + else { + float beta = asin (fabs (cos (r->deg * PI / 180) / this->n) )* 180 / PI; + if ((((fabs((this->x1 - p->x)) * tan ((90-beta) * PI / 180)) >= this->l) && (r->x >= p->x)) || (((fabs((this->x3 - p->x)) * tan ((90-beta) * PI / 180)) >= this->l) && (r->x <= p->x))) { + float beta = asin (fabs (cos (r->deg * PI / 180) / this->n) )* 180 / PI; + if (r->y < p->y){ + if (r->x >= p->x) { + new_p->y = p->y + this->l; + new_p->x = p->x - this->l * tan ( beta * PI / 180); + } + else { + beta = fabs (beta); + new_p->y = p->y + this->l; + new_p->x = p->x + this->l * tan ( beta * PI / 180); + } + } + + else { + if (r->x >= p->x) { + new_p->y = p->y - this->l; + new_p->x = p->x - this->l * tan ( beta * PI / 180); + } + else { + beta = fabs (beta); + new_p->y = p->y - this->l; + new_p->x = p->x + this->l * tan ( beta * PI / 180); + } + } + deg = r->deg; + } + else { + //cout << "yes \n" ; + if (r->y < p->y){ + if (r->x >= p->x) { + new_p->y = p->y + fabs((this->x1 - p->x)) * tan ((90-beta) * PI / 180); + new_p->x = this->x1; + if ((cos (beta * PI / 180) * this->n) <= 1) + deg = 180 + asin (cos (beta * PI / 180) * this->n) * 180 / PI; + else + deg = 270; + } + else { + beta = fabs (beta); + new_p->y = p->y + fabs((this->x3 - p->x)) * tan ((90-beta) * PI / 180); + new_p->x = this->x3; + if ((cos (beta * PI / 180) * this->n) <= 1) + deg = 360 - asin (cos (beta * PI / 180) * this->n) * 180 / PI; + else + deg = 270; + } + } + // HERE YOU STOPPED + else { + if (r->x >= p->x) { + new_p->y = p->y - fabs((this->x1 - p->x)) * tan ((90-beta) * PI / 180); + new_p->x = this->x1; + if ((cos (beta * PI / 180) * this->n) <= 1) + deg = 180 - asin (cos (beta * PI / 180) * this->n) * 180 / PI; + else + deg = 90; + } + else { + beta = fabs (beta); + new_p->y = p->y - fabs((this->x3 - p->x)) * tan ((90-beta) * PI / 180); + new_p->x = this->x3; + if ((cos (beta * PI / 180) * this->n) <= 1) + deg = asin (cos (beta * PI / 180) * this->n) * 180 / PI; + else + deg = 90; + } + } + } + } + //cout << "beta = " << beta << "\n"; + r->x = new_p->x * cos (this->deg * PI / 180) + new_p->y * sin (this->deg * PI / 180) ; + r->y = new_p->y * cos (this->deg * PI / 180) - new_p->x * sin (this->deg * PI / 180); + r->deg = deg + this->deg; + if (r->deg > 360) + r->deg = r->deg - 360; + + } + + + ~Disc () + { + cout << "Destructure of the Disc" << "\n"; + } + + +}; + + + + +/* +class Lens_ras : public Device{ +public: + float deg, f, l; + +public: + Lens_ras ( float x, float y, float l_0, float deg_0, float f_0){ //deg from vertical 0 <= deg < 90 !!! against hour ; + x1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180) ; + x2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180); + y1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180); + y2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180); + l = l_0; + f = f_0; + deg = deg_0; + } + point * cross_point (RAY * r) const { + point * p = new point (); + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))){ + cout << "c_here_1 \n"; + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { + cout << "c_here_2 \n"; + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + return NULL; + } + + void change_direction(RAY * r, point * p ) const + { + float l_1 = 0; + float alpha = 0; + float alpha_r = 0; + float c = 0 ; + float length = (float) sqrt ((r->x - (this->x1 + this->x2)/2) * (r->x - (this->x1 + this->x2)/2)+ (r->y - (this->y1 + this->y2)/2) * (r->y - (this->y1 + this->y2)/2)) ; + float line_tg = fabs ((float)((r->y - (this->y1 + this->y2)/2) / ((this->x1 + this->x2)/2 - r->x))) ; + //cout << " line_tg_prev = " << line_tg << "\n"; + float line_deg = atan (line_tg) * 180 / PI; + cout << "line_deg_prev = " << line_deg <<"\n"; + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) { + if (r->y <= (this->y1 + this->y2)/2) + line_deg = this->deg + line_deg; + else + line_deg = fabs (line_deg - this->deg); + } + else { + if (r->y >= (this->y1 + this->y2)/2) + line_deg = this->deg + line_deg; + else + line_deg = fabs (line_deg - this->deg); + } + + cout << "line_deg = " << line_deg << "\n"; + line_tg = tan (line_deg * PI / 180); + + //cout << " line_tg = " << line_tg << "\n"; + float a = length * cos (atan (line_tg)); + //cout << " a = " << a << "\n"; + float b = (float) (this->f * a / (a + this->f)); + //cout << " b = " << b << "\n"; + c = sqrt ((p->x - (this->x1 + this->x2)/2) * (p->x - (this->x1 + this->x2)/2)+ (p->y - (this->y1 + this->y2)/2) * (p->y - (this->y1 + this->y2)/2)) ; + + if (((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) || ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0))) { // точка пересечения и координата луча по разные стороны от опт.оси + l_1 = c + (float) b * line_tg; + cout << "here \n"; + alpha = atan ((float) (l_1 / b)) * 180 / PI; + alpha_r = 180 -alpha ; + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2/cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2/cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) { //точка координата луча выше точка пересечения ниже + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << " here_0 \n"; + } + + } + else { //точка персечкения и полоожение луча по одну сторону от опт.оси + l_1 = fabs (c - (float) b * line_tg); + alpha = atan ((float) (l_1 / b)) * 180 / PI; + alpha_r = 180 -alpha ; + cout << "here_here \n"; + if ((orient (this->x1, this->y1 + this->l / 2/cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)){ // луч выше опт.оси точка пересечения под всмогат.линией сверху + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "here_1 \n"; + + } + + + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) { // луч ниже опт.оси точка пересечения над всмогат.линией снизу + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "alpha =" << alpha << "\n"; + cout << "here_2 \n"; + } + + } + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) + r->deg = alpha ; + else + r->deg = alpha_r; + + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) { + if ((r->deg + this->deg - 360) > 0) + r->deg = (r->deg + this->deg - 360); + else + r->deg = r->deg + this->deg; + } + else { + + r->deg = r->deg + this->deg; + } + + r->x = p->x; + r->y = p->y; + } + const char *getID() const{ + return "Lens_ras"; + } + + ~Lens_ras () + { + cout << "Destructure of the Lens_ras" << "\n"; + } + + +}; +*/ + + +class Lens : public Device{ +public: + float deg, f, l; + +public: + Lens( float x, float y, float l_0, float deg_0, float f_0){ //deg from vertical 0 <= deg < 90 !!! against hour ; + x1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180) ; + x2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180); + y1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180); + y2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180); + l = l_0; + f = f_0; + deg = deg_0; + } + point * cross_point (RAY * r) const { + point * p = new point (); + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))){ + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + cout << "p->x = " << p->x << "\n" ; + p->y = (float)det_2/det; + cout << "p->x = " << p->x << "\n" ; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + return NULL; + } + + void change_direction(RAY * r, point * p ) const + { + float l_1 = 0; + float alpha = 0; + float alpha_r = 0; + float c = 0 ; + float length = (float) sqrt ((r->x - (this->x1 + this->x2)/2) * (r->x - (this->x1 + this->x2)/2)+ (r->y - (this->y1 + this->y2)/2) * (r->y - (this->y1 + this->y2)/2)) ; + float line_tg = fabs ((float)((r->y - (this->y1 + this->y2)/2) / ((this->x1 + this->x2)/2 - r->x))) ; + //cout << " line_tg_prev = " << line_tg << "\n"; + float line_deg = atan (line_tg) * 180 / PI; + cout << "line_deg_prev = " << line_deg <<"\n"; + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) { + if (r->y <= (this->y1 + this->y2)/2) + line_deg = this->deg + line_deg; + else + line_deg = fabs (line_deg - this->deg); + } + else { + if (r->y >= (this->y1 + this->y2)/2) + line_deg = this->deg + line_deg; + else + line_deg = fabs (line_deg - this->deg); + } + + cout << "line_deg = " << line_deg << "\n"; + line_tg = tan (line_deg * PI / 180); + + //cout << " line_tg = " << line_tg << "\n"; + float a = length * cos (atan (line_tg)); + //cout << " a = " << a << "\n"; + if (a != this->f){ + if (a > this->f) { + float b = (float) (this->f * a / (a - this->f)); + cout << " b = " << b << "\n"; + c = sqrt ((p->x - (this->x1 + this->x2)/2) * (p->x - (this->x1 + this->x2)/2)+ (p->y - (this->y1 + this->y2)/2) * (p->y - (this->y1 + this->y2)/2)) ; + + if (((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - l / 2 / cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0)) || ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0))) { // точка пересечения и координата луча выше или ниже опт.оси + l_1 = c + (float) b * line_tg; + cout << "here \n"; + alpha = atan ((float) (l_1 / b)) * 180 / PI; + alpha_r = 180 -alpha ; + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2/cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2/cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0)) { //точка пересечения и координата луча выше + + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + + cout << " here_0 \n"; + } + + } + else { //точка персечкения и полоожение луча по разные стороны от опт.оси + l_1 = fabs (c - (float) b * line_tg); + alpha = atan ((float) (l_1 / b)) * 180 / PI; + cout << "here \n"; + if ((orient (this->x1, this->y1 + this->l / 2/cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0)){ // луч выше опт.оси точка пересечения под всмогат.линией снизу + + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "here_1 \n"; + + } + + + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0)) { // луч ниже опт.оси точка пересечения за всмогат.линией сверху + + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "alpha =" << alpha << "\n"; + cout << "here_2 \n"; + } + + } + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) + r->deg = alpha ; + else + r->deg = alpha_r; + } + else { // a < f + cout << "here_3\n"; + float b = fabs((float) (this->f * a / (a - this->f))); + //cout << " b = " << b << "\n"; + c = sqrt ((p->x - (this->x1 + this->x2)/2) * (p->x - (this->x1 + this->x2)/2)+ (p->y - (this->y1 + this->y2)/2) * (p->y - (this->y1 + this->y2)/2)) ; + cout << "c = " << c << "\n"; + + if (((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) || ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0))) { // точка пересечения и координата луча по разные стороны от опт.оси + cout << "here_4\n"; + l_1 = c + (float) b * line_tg; + alpha = atan ((float) (l_1 / b)) * 180 / PI; + alpha_r = 180 - alpha; + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)){ // координата луча выше опт.оси точка персечения ниже опт.оси + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "here_5\n"; + cout << "alpha = " << alpha << "\n"; + + } + + } + else { // по одну сторону + cout << "here_6\n"; + l_1 = fabs (c - (float) b * line_tg); + alpha = atan ((float) (l_1 / b)) * 180 / PI; + alpha_r = 180 -alpha ; + /*cout << " x1 = " << this->x1 << "\n"; + cout << " y1 = " << this->y1 + (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180) << "\n"; + cout << " x2 = " << this->x2 << "\n"; + cout << " y2 = " << this->y2 - (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180) << "\n"; + cout << " x3 = " << p->x << "\n"; + cout << " y3 = " << p->y << "\n";*/ + + + + + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) { // луч выше опт.оси пересечение под вспомагат.линии сверху + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "here_7\n"; + + } + + + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) { // луч ниже опт.оси пересечение за вспомагат.линии снизу + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "here_8\n"; + + } + } + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) + r->deg = alpha ; + else + r->deg = alpha_r; + } + } + else { // a = f + alpha = atan (line_tg) * 180 / PI; + alpha_r = alpha; + if (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0){ + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + } + + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) + r->deg = alpha ; + else + r->deg = alpha_r; + + } + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) { + if ((r->deg + this->deg - 360) > 0) + r->deg = (r->deg + this->deg - 360); + else + r->deg = r->deg + this->deg; + } + else { + + r->deg = r->deg + this->deg; + } + + r->x = p->x; + r->y = p->y; + } + const char *getID() const{ + return "Lens"; + } + + ~Lens () + { + cout << "Destructure of the Lens" << "\n"; + } +}; + + + +class Lens_wide : public Device{ +public: + float deg, l,r1,r2,n,d; + +public: + Lens_wide( float x, float y, float l_0, float deg_0, float r1_0, float r2_0, float n_0, float d_0){ //deg from vertical 0 <= deg < 90 !!! against hour ; + x1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180) ; + x2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180); + y1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180); + y2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180); + l = l_0; + r1 = r1_0; + r2 = r2_0; + n = n_0; + d = d_0; + deg = deg_0; + } + point * cross_point (RAY * r) const { + point * p = new point (); + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))){ + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + cout << "p->x = " << p->x << "\n" ; + p->y = (float)det_2/det; + cout << "p->x = " << p->x << "\n" ; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + return NULL; + } + + void change_direction(RAY * r, point * p ) const + { + float l_1 = 0; + float alpha = 0; + float alpha_r = 0; + float c = 0 ; + float length = (float) sqrt ((r->x - (this->x1 + this->x2)/2) * (r->x - (this->x1 + this->x2)/2)+ (r->y - (this->y1 + this->y2)/2) * (r->y - (this->y1 + this->y2)/2)) ; + float line_tg = fabs ((float)((r->y - (this->y1 + this->y2)/2) / ((this->x1 + this->x2)/2 - r->x))) ; + //cout << " line_tg_prev = " << line_tg << "\n"; + float line_deg = atan (line_tg) * 180 / PI; + cout << "line_deg_prev = " << line_deg <<"\n"; + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) { + if (r->y <= (this->y1 + this->y2)/2) + line_deg = this->deg + line_deg; + else + line_deg = fabs (line_deg - this->deg); + } + else { + if (r->y >= (this->y1 + this->y2)/2) + line_deg = this->deg + line_deg; + else + line_deg = fabs (line_deg - this->deg); + } + + cout << "line_deg = " << line_deg << "\n"; + line_tg = tan (line_deg * PI / 180); + + //cout << " line_tg = " << line_tg << "\n"; + float a = length * cos (atan (line_tg)); + //cout << " a = " << a << "\n"; + float f = 0; + if (orient (this->x1, this->y1, this->x2, this->y2, r->x, r->y,1) < 0) + f = (float) 1 / ((this->n -1) * ((float)1/this->r1 - (float)1/this->r2 + (float) ((this->n-1) * this->d) / (this->n * this->r1 *this->r2))) ; + else + f = (float) 1 / ((this->n -1) * ((float)1/this->r2 - (float)1/this->r1 + (float) ((this->n-1) * this->d) / (this->n * this->r1 *this->r2))) ; + if (a != this->f){ + if (a > f) { + float b = (float) (f * a / (a - f)); + cout << " b = " << b << "\n"; + c = sqrt ((p->x - (this->x1 + this->x2)/2) * (p->x - (this->x1 + this->x2)/2)+ (p->y - (this->y1 + this->y2)/2) * (p->y - (this->y1 + this->y2)/2)) ; + + if (((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - l / 2 / cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0)) || ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0))) { // точка пересечения и координата луча выше или ниже опт.оси + l_1 = c + (float) b * line_tg; + cout << "here \n"; + alpha = atan ((float) (l_1 / b)) * 180 / PI; + alpha_r = 180 -alpha ; + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2/cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2/cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0)) { //точка пересечения и координата луча выше + + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + + cout << " here_0 \n"; + } + + } + else { //точка персечкения и полоожение луча по разные стороны от опт.оси + l_1 = fabs (c - (float) b * line_tg); + alpha = atan ((float) (l_1 / b)) * 180 / PI; + cout << "here \n"; + if ((orient (this->x1, this->y1 + this->l / 2/cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0)){ // луч выше опт.оси точка пересечения под всмогат.линией снизу + + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "here_1 \n"; + + } + + + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0)) { // луч ниже опт.оси точка пересечения за всмогат.линией сверху + + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "alpha =" << alpha << "\n"; + cout << "here_2 \n"; + } + + } + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) + r->deg = alpha ; + else + r->deg = alpha_r; + } + else { // a < f + cout << "here_3\n"; + float b = fabs((float) (f * a / (a - f))); + //cout << " b = " << b << "\n"; + c = sqrt ((p->x - (this->x1 + this->x2)/2) * (p->x - (this->x1 + this->x2)/2)+ (p->y - (this->y1 + this->y2)/2) * (p->y - (this->y1 + this->y2)/2)) ; + cout << "c = " << c << "\n"; + + if (((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) || ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0))) { // точка пересечения и координата луча по разные стороны от опт.оси + cout << "here_4\n"; + l_1 = c + (float) b * line_tg; + alpha = atan ((float) (l_1 / b)) * 180 / PI; + alpha_r = 180 - alpha; + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)){ // координата луча выше опт.оси точка персечения ниже опт.оси + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "here_5\n"; + cout << "alpha = " << alpha << "\n"; + + } + + } + else { // по одну сторону + cout << "here_6\n"; + l_1 = fabs (c - (float) b * line_tg); + alpha = atan ((float) (l_1 / b)) * 180 / PI; + alpha_r = 180 -alpha ; + /*cout << " x1 = " << this->x1 << "\n"; + cout << " y1 = " << this->y1 + (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180) << "\n"; + cout << " x2 = " << this->x2 << "\n"; + cout << " y2 = " << this->y2 - (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180) << "\n"; + cout << " x3 = " << p->x << "\n"; + cout << " y3 = " << p->y << "\n";*/ + + + + + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) { // луч выше опт.оси пересечение под вспомагат.линии сверху + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "here_7\n"; + + } + + + if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) { // луч ниже опт.оси пересечение за вспомагат.линии снизу + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + cout << "here_8\n"; + + } + } + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) + r->deg = alpha ; + else + r->deg = alpha_r; + } + } + else { // a = f + alpha = atan (line_tg) * 180 / PI; + alpha_r = alpha; + if (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0){ + alpha_r = alpha_r + 2 * alpha; + alpha = 360 - alpha; + } + + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) + r->deg = alpha ; + else + r->deg = alpha_r; + + } + if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) { + if ((r->deg + this->deg - 360) > 0) + r->deg = (r->deg + this->deg - 360); + else + r->deg = r->deg + this->deg; + } + else { + + r->deg = r->deg + this->deg; + } + + r->x = p->x; + r->y = p->y; + } + const char *getID() const{ + return "Lens_wide"; + } + + ~Lens_wide () + { + cout << "Destructure of the Lens" << "\n"; + } +}; +//1) Плоское зеркало (XY – координаты; lenght – длина зеркала; deg_0 – угол поворота). +class PlainRefl : public Device { + public: + float deg, f; + +public: + PlainRefl( float x, float y, float l, float deg_0){ //deg from vertical 0 <= deg <= 90 against hour ; + x1 = x - (float)(l/2) * sin (GradToRad(deg_0)) ; + x2 = x + (float)(l/2) * sin (GradToRad(deg_0)); + y1 = y - (float)(l/2) * cos (GradToRad(deg_0)); + y2 = y + (float)(l/2) * cos (GradToRad(deg_0)); + deg = deg_0; + std::cout << "Create PlainRefl (" << x1 << ", " << y1 << "), (" << x2 << ", " << y2 << ") deg: " << deg << "\n"; + } + point *cross_point(RAY *r) const { + point *p = new point(); + if ((( this->x1 + this->x2 )/2 > r->x) && ((r->deg <= 90) || (r->deg > 270))){ + float det = this->y2 - this->y1 - tan (GradToRad(r->deg)) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 + - this->y1 * this->x2 + - (this->x1 - this->x2) * (r->y + tan (GradToRad(r->deg)) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (GradToRad(r->deg)) * r->x) + - (this->y2 * this->x1 - this->y1 * this->x2) * tan (GradToRad(r->deg)); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if ((( p->x >= x1 ) && ( p->x <= x2 ) && (x1 <= x2)) && (( p->y >= y1 ) && ( p->y <= y2 ) && (y1 <= y2))) return p; + if ((( p->x <= x1 ) && ( p->x >= x2 ) && (x1 >= x2)) && (( p->y >= y1 ) && ( p->y <= y2 ) && (y1 <= y2))) return p; + if ((( p->x >= x1 ) && ( p->x <= x2 ) && (x1 <= x2)) && (( p->y <= y1 ) && ( p->y >= y2 ) && (y1 >= y2))) return p; + if ((( p->x <= x1 ) && ( p->x >= x2 ) && (x1 >= x2)) && (( p->y <= y1 ) && ( p->y >= y2 ) && (y1 >= y2))) return p; + } + if ((( this->x1 + this->x2 )/2 < r->x) && ((r->deg >= 90) && (r->deg <= 270))) { + float det = this->y2 - this->y1 - tan (GradToRad(r->deg)) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (GradToRad(r->deg)) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (GradToRad(r->deg)) * r->x) + - (this->y2 * this->x1 - this->y1 * this->x2) * tan (GradToRad(r->deg)); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if ((( p->x >= x1 ) && ( p->x <= x2 ) && (x1 <= x2)) && (( p->y >= y1 ) && ( p->y <= y2 ) && (y1 <= y2))) return p; + if ((( p->x <= x1 ) && ( p->x >= x2 ) && (x1 >= x2)) && (( p->y >= y1 ) && ( p->y <= y2 ) && (y1 <= y2))) return p; + if ((( p->x >= x1 ) && ( p->x <= x2 ) && (x1 <= x2)) && (( p->y <= y1 ) && ( p->y >= y2 ) && (y1 >= y2))) return p; + if ((( p->x <= x1 ) && ( p->x >= x2 ) && (x1 >= x2)) && (( p->y <= y1 ) && ( p->y >= y2 ) && (y1 >= y2))) return p; + } + return NULL; + } + + void change_direction(RAY *r, point *p) const // Плоское зеркало + { + r->x = p->x; + r->y = p->y; + r->deg = (90 + this->deg) * 2 - r->deg; + + r->deg = r->Deg360(r->deg); + } + const char *getID() const { + return "PlainRefl"; + } + + ~PlainRefl () + { + cout << "Destructure of the PlainRefl" << "\n"; + } +}; + + +//2) Сферическое зеркало ( +// XY – координаты; +// lenght – длина зеркала; +// R – радиус кривизны; +// deg_1, deg_2 – углы, под которыми видны нижняя и верхняя границы зеркала). + +class SphereRefl : public Device { + public: + float deg1, deg2, R; + point *OptC; + SphereRefl( float x, float y, float R0, float deg_1, float deg_2) + { + OptC = new point(); //deg from vertical 0 <= deg <= 90 against hour ; + OptC->x = x; + OptC->y = y; + R = R0; // Радиус кривизны сферы + x1 = OptC->x - R * sin (GradToRad(deg_1)) ; + x2 = OptC->x - R * sin (GradToRad(deg_2)); + y1 = OptC->y - R * cos (GradToRad(deg_1)); + y2 = OptC->y - R * cos (GradToRad(deg_2)); + this->f = fabs(this->R); // фокусное расстояние + deg1 = deg_1; + deg2 = deg_2; + std::cout << "\nCreate SphereRefl x: " << x << " y: " << y << " R: " << R << " deg1:" << deg1 << " deg2: " << deg2 << "\n"; + std::cout << "X1:" << x1 << " Y1: " << y1 << " X2: " << x2 << " Y2: " << y2 << "\n"; + } + + point *cross_point(RAY *r) const { // точка пересечения сферы и луча + point *p = new point(); + + float xn, yn, k_ray, b_ray; + float xc, yc; + if (r->deg != 0) { + xn = r->y / tan (GradToRad(r->deg)) + r->x; + yn = r->x * tan (GradToRad(r->deg)) + r->y; + + k_ray = -yn/xn; // уравнение луча + b_ray = yn; + // пересечение луча и нормали, проходящей через центр сферы + xc = (OptC->y - OptC->x/k_ray - yn) / (k_ray + 1/k_ray); // k_ray = 0 ? + yc = -(xc + OptC->x)/ k_ray + OptC->y; + } else { + xc = OptC->x; + yc = r->y; + } + point pc; + pc.x = xc; + pc.y = yc; + point p_ray; + p_ray.x = r->x; + p_ray.y = r->y; + float l_ray_optc = LenVect(&p_ray, OptC); + float l_ray_pc = LenVect(&p_ray, &pc); + float l_d = sqrt(l_ray_optc * l_ray_optc - l_ray_pc * l_ray_pc); + float l_ray; + if (this->R >= l_d) { + l_ray = fabs(l_ray_pc - sqrt(R * R - l_d * l_d)); + p->x = r->x + l_ray * cos(GradToRad(r->deg)); + p->y = r->y - l_ray * sin(GradToRad(r->deg)); + + float delta = Sqr(p->x - OptC->x) + Sqr(p->y - OptC->y) - Sqr(R); + std::cout << "1) p->x: " << p->x << " p->y: " << p->y << " r->deg: " << r->deg << " delta:" << delta << "\n"; + // Проверка принадлежности сегменту + // Находим угол от вертикали + float deg_t; + if (fabs(delta) < 0.1) { + deg_t = RadToGrad(atan((OptC->x - p->x) / (OptC->y - p->y))); + if (OptC->x - p->x < 0) deg_t += 180; + deg_t = r->Deg360(deg_t); + + std::cout << "Sphere Point 1: x=" << p->x << " y=" << p->y << " Sphere Deg=" << deg_t << " deg1=" << deg1 << " deg2=" << deg2 << "\n"; + if (deg1 <= deg_t && deg_t <= deg2 ) return p; + } + + // Второй корень + l_ray = fabs(l_ray_pc + sqrt(R * R - l_d * l_d)); + p->x = r->x + l_ray * cos(GradToRad(r->deg)); + p->y = r->y - l_ray * sin(GradToRad(r->deg)); + + delta = Sqr(p->x - OptC->x) + Sqr(p->y - OptC->y) - Sqr(R); + std::cout << "2) p->x: " << p->x << " p->y: " << p->y << " r->deg: " << r->deg << " delta:" << delta << "\n"; + + if (fabs(delta) < 0.1) { + deg_t = RadToGrad(atan((OptC->x - p->x) / (OptC->y - p->y))); + deg_t = r->Deg360(deg_t); + + std::cout << "Sphere Point 2: x=" << p->x << " y=" << p->y << " Sphere Deg=" << deg_t << " deg1=" << deg1 << " deg2=" << deg2 << "\n"; + if (deg1 <= deg_t && deg_t <= deg2 ) return p; + } + } + + return NULL; + } + + void change_direction(RAY *r, point *p) const // Сферическое зеркало + { + std::cout << "\nPrev r->deg: " << r->deg << "\n"; + float a1 = p->x - r->x; + float b1 = r->y - p->y; + float a2 = p->x - OptC->x; + float b2 = OptC->y - p->y; + float deg_f = RadToGrad(atan((a1 * b2 - a2 * b1) / (a1 * a2 + b1 * b2))); // угол между лучом и нормалью к сфере + std::cout << "Deg ray-normal: " << deg_f << "\n"; + + + r->x = p->x; + r->y = p->y; + r->deg = r->deg + (180 - 2 * fabs(deg_f)); + r->deg = r->Deg360(r->deg); + std::cout << "New r->deg: " << r->deg << "\n"; + } + + const char *getID() const { + return "SphereRefl"; + } + + ~SphereRefl() + { + cout << "Destructure of the " << this->getID() << "\n"; + } +}; +class Gran { +public: + Prism *prism; + point *p1,*p2; + point *normal; + Gran(Prism *_prism, point *_p1, point *_p2) + { + prism = _prism; + p1 = new point; + p2 = new point; + p1->x = _p1->x; + p1->y = _p1->y; + p2->x = _p2->x; + p2->y = _p2->y; + normal = new point(); + normal->x = (p2->y - p1->y); + normal->y = (p1->x - p2->x); + } + + float CheckPointCosA (point *pp) const // точка pp внутри [p1,p2] + { + return Scalar(SubVect(p1, pp), SubVect(p2, pp)) / (LenVect(pp, p1) * LenVect(pp, p2)); + } + + bool CheckPoint (point *pp) const // точка pp внутри [p1,p2] + { + float cos_a = Scalar(SubVect(p1, pp), SubVect(p2, pp)) / (LenVect(pp, p1) * LenVect(pp, p2)); + if (cos_a < 0) return true; + return false; + } + + point *cross_point(RAY *r) const + { // точка пересечения грани призмы и луча + point *p = new point(); + float p_check = (r->x - p1->x) * (p2->y - p1->y) - (r->y - p1->y) * (p2->x - p1->x); // источник луча находится на грани призмы + if (fabs(p_check) < 0.1) return NULL; + float x1, y1, x2, y2, r1, r2, k_ray, b_ray; + float det, det1, det2; + float xp, yp; + + if (r->deg != 90 && r->deg != 270) { // tan 90 + x1 = p2->y - p1->y; + y1 = -(p2->x - p1->x); + r1 = p1->x * x1 + p1->y * y1; + + x2 = tan(GradToRad(r->deg)); + y2 = 1; + r2 = r->x * x2 + r->y; + + det = x1 * y2 - x2 * y1; // определитель - метод Крамера + det1 = r1 * y2 - r2 * y1; + det2 = x1 * r2 - x2 * r1; + + if (det == 0) return NULL; // прямые параллельны + p->x = det1 / det; + p->y = det2 / det; + } + else // луч параллелен оси y + { + x1 = p2->y - p1->y; + y1 = -(p2->x - p1->x); + r1 = p1->x * x1 + p1->y * y1; + + p->x = r->x; + p->y = (r1 - r->x * x1) / y1; + } + if (CheckPoint (p)) return p; + + + return NULL; + } +}; +//5) Призма (n - № объекта; XY– координаты; n – показатель преломления). +class Prism : public Device { +public: + int N; + float len, p, a , b, n_coeff; + vector gran_vect; + vector P; + float deg1, deg2; + point *OC; + + Prism(int _N, float _x1, float _y1, float _x2, float _y2, float _x3, float _y3, float _n_coeff) + { + N = _N; + n_coeff = _n_coeff; + Gran *gran_t; + point *p_t = new point; + p_t->x = _x1; + p_t->y = _y1; + P.push_back(p_t); + p_t = new point; + p_t->x = _x2; + p_t->y = _y2; + P.push_back(p_t); + p_t = new point; + p_t->x = _x3; + p_t->y = _y3; + P.push_back(p_t); + gran_t = new Gran((Prism *)this, P[0], P[1]); + gran_vect.push_back(gran_t); + gran_t = new Gran((Prism *)this, P[1], P[2]); + gran_vect.push_back(gran_t); + gran_t = new Gran((Prism *)this, P[2], P[0]); + gran_vect.push_back(gran_t); + + std::cout << "\n Create Prism N: " << N << "\n"; + for (int i = 0; i < P.size();i++) { + std::cout << "Point " << i << " x: " << P[i]->x << " y: " << P[i]->y << "\n"; + } + std::cout << "Coeff_N: " << n_coeff << "\n"; + } + + point *cross_point(RAY *r) const { // точка пересечения призмы и луча + point *p = NULL; + float len_min = 1e10; + + for (int i = 0; i < gran_vect.size(); i++) { + point *p_t = gran_vect[i]->cross_point(r); // проверка грани призмы и луча + if (p_t != NULL) { + if (r->CheckRayPoint(p_t)) { + point *pr = new point(); + pr->x = r->x; + pr->y = r->y; + float len_t = LenVect(p_t, pr); // минимальное расстояние до источника + if (len_min > len_t) { + len_min = len_t; + p = new point(); + p->x = p_t->x; + p->y = p_t->y; + } + } + } + } + if (p != NULL) { + std::cout << "Prism intersection \n"; + std::cout << "p->x: " << p->x << " p->y: " << p->y << " r->deg: " << r->deg << "\n"; + return p; + } + + return NULL; + } + + Gran *FindGran(point *pp) const + { + float cos_a = 0; + for (int i=0; i < gran_vect.size(); i++) { // нашли грань для точки p + cos_a = gran_vect[i]->CheckPointCosA(pp); + if (fabs(cos_a + 1) < 0.1) + { + return gran_vect[i]; + break; + } + } + return NULL; + } + + void change_direction(RAY *r, point *p) const // Призма + { + std::cout << "\nPrev r->deg: " << r->deg << "\n"; // исходный угол луча + std::cout << "Point_in: x: " << p->x << " y: " << p->y << " \n"; + std::cout << "Coeff n : " << n_coeff << " \n"; + float cos_a = 0; + Gran *gran = FindGran(p); + + point *r0 = new point(); + r0->x = r->x; + r0->y = r->y; + + cos_a = Scalar(SubVect(p, r0), gran->normal) / (LenVect(p, r0) * sqrt(Sqr(gran->normal->x) + Sqr(gran->normal->y))); + float deg_f = RadToGrad(acos(cos_a)); + std::cout << "Deg_in: " << deg_f << "\n"; + while (fabs(deg_f) > 90 ) { + deg_f = 180 - deg_f; + std::cout << "Deg_in: " << deg_f << " 180-a \n"; + } + float deg_r = RadToGrad(asin(sin(GradToRad(deg_f)) / n_coeff)); + std::cout << "Deg_r_in: " << deg_r << "\n"; + while (fabs(deg_r) > 90 ) { + deg_r = 180 - deg_r; + std::cout << "Deg_r_in: " << deg_r << " 180-a \n"; + } + + float deg_r_new = r->deg - deg_f + deg_r; + + std::cout << "Deg_ray_new " << deg_r_new << "\n"; + + RAY *ray_in = new RAY; + ray_in->set_ray_pos (p->x, p->y, ray_in->Deg360(deg_r_new)); + + point *p_new = cross_point(ray_in); // выходная точка + if (p_new != NULL) + { + Gran *gran_out = FindGran(p_new); + std::cout << "\n Point_out: x: " << p_new->x << " y: " << p_new->y << "\n"; + + cos_a = Scalar(SubVect(p_new, r0), gran_out->normal) / (LenVect(p_new, r0) * sqrt(Sqr(gran_out->normal->x) + Sqr(gran_out->normal->y))); + float deg_r_out = RadToGrad(acos(cos_a)); // угол между лучом и выходной нормалью + std::cout << " Deg_r_out: " << deg_r_out << "\n"; + while (fabs(deg_r_out) > 90 ) { + deg_r_out = 180 - deg_r_out; // 0..90 + std::cout << " Deg_r_out: " << deg_r_out << " 180-a \n"; + } + if (sin(GradToRad(deg_r_out)) * n_coeff > 1) { + std::cout << " sin(Deg_i_out) * n > 1: " << sin(GradToRad(deg_r_out)) * n_coeff << "\n"; + deg_r_out = 90 - deg_r_out; + } + float deg_i_out = RadToGrad(asin(sin(GradToRad(deg_r_out)) * n_coeff)); // sin(i_out) = sin(r_out) * n + std::cout << " Deg_i_out: " << deg_i_out << "\n"; + while (fabs(deg_i_out) > 90 ) { + deg_i_out = 180 - deg_i_out; // 0..90 + std::cout << " Deg_i_out: " << deg_i_out << " 180-a \n"; + } + + float deg_i_out_new = ray_in->deg - deg_i_out + deg_r_out; + std::cout << "Deg_i_out_new: " << deg_i_out_new << "\n"; + + r->deg = r->Deg360(deg_i_out_new); + r->x = p_new->x; + r->y = p_new->y; + } + r->x = p->x; + r->y = p->y; + r->deg = r->Deg360(r->deg); // угол луча в [0..360] + std::cout << "New r->deg: " << r->deg << "\n"; + } + + + const char *getID() const { + return "Prism"; + } + + ~Prism() + { + cout << "Destructure of the " << this->getID() << "\n"; + } +}; + + diff --git a/051315/main/first.h b/051315/main/first.h new file mode 100644 index 0000000..b7e54ff --- /dev/null +++ b/051315/main/first.h @@ -0,0 +1,71 @@ +#include +#include "device.h" +#include + + +float min_(float x1, float x2){ + if (x1 < x2) + return x1; + else return x2; +} +void print_(vector my_device){ + for (int i = 0; i < my_device.size(); i++){ + printf("%d:%f\n", i,min_(my_device[i]->x1, my_device[i]->x2)); + } + printf("\n"); +} +float sqr_(float x){ + return x*x; +} + +int first(vector d, RAY *r){ + int ret = -1; + float x = r->x; + float y = r->y; + float rast; + float min = 100000; + if (d.size==0){ + return ret; + } + for (int i = 0; i < d.size(); i++){ + point *cross = NULL; + cross = d[i] -> cross_point(r); + if (cross != NULL){ + float xp = cross -> x; + float yp = cross -> y; + rast = sqrt(sqr_(x - xp)+sqr_(y - yp)); + } + else{ + rast = 1000000; + } + if (rast < min){ + min = rast; + ret = i; + } + } + return ret; +} +int first_s(vector d, RAY *r){ + int ret = -1; + float x = r->x; + float y = r->y; + float rast; + float min = 100000; + for (int i = 0; i < d.size(); i++){ + point *cross = NULL; + cross = d[i] -> cross_point(r); + if (cross != NULL){ + float xp = cross -> x; + float yp = cross -> y; + rast = sqrt(sqr_(x - xp)+sqr_(y - yp)); + } + else{ + rast = 1000000; + } + if (rast < min){ + min = rast; + ret = i; + } + } + return ret; +} diff --git a/051315/main/main.cc b/051315/main/main.cc new file mode 100644 index 0000000..a3290a0 --- /dev/null +++ b/051315/main/main.cc @@ -0,0 +1,280 @@ +#include +#include +#include +#include +#include +#include +#include +#include +//#include "ray.h" //was included in "device.h" +#include +#include +#include "first.h" + + +int h; + +void handler(int nsig){ + if(nsig==SIGINT){ + close(h); + printf("\nShutting down\n"); + exit(0); + } +} +void *func(void *arg); +int main() +{ + (void)signal(SIGINT, handler); + h = socket(PF_INET, SOCK_STREAM, 0); + if (h < 0){ + perror("Creating socket"); + } + const int PORT = 5678; + struct sockaddr_in local; + local.sin_family = PF_INET; + local.sin_port = htons(PORT); + if(bind(h, (sockaddr *)&local, sizeof(local)) < 0){ + perror("Binding"); + close(h); + return 1; + } + if (listen(h, 10) < 0){ + perror("Listening"); + close(h); + return 2; + } + while(1) + { + struct sockaddr_in remote; + unsigned remoteLen=sizeof(remote); + int cs; + if((cs=accept(h, (sockaddr *)&remote, &remoteLen))<0){ + perror("Accepting"); + } + pthread_t thread; + if(pthread_create(&thread, NULL, func, &cs)<0) + { + perror("Thread: "); + } + } + close(h); +} + +void *func(void* arg) +{ +int rd; +char buf[512]; +int cs=*(int *)arg; + char buf_[32]; + + vector my_device; + vector my_screen; + vector my_laser; + vector my_source; + if(send(cs, "1", 1, MSG_NOSIGNAL)==-1) + { + perror("Can't send:"); + return NULL; + } + while((rd=recv(cs, buf, sizeof(buf), 0))>0){ + buf[rd]=0; + printf("%s\n", buf); + int check; + + if ((buf[1]>='0')&&(buf[1]<='9')){ + int check_10 = buf[0] - '0'; + check_10 = check_10 * 10; + int check_1 = buf[1] - '0'; + check = check_10 + check_1; + printf("check = %d\n", check); + } + else{ + check = buf[0] - '0'; + } + switch(check){ + case 0: //source + { + float a1,x,y; + sscanf(buf, "%f %f %f", &a1, &x, &y); + SOURCE* d=new SOURCE(x,y); + my_source.push_back(d); + printf("New source was created\n"); + break; + } + case 1: //screen + { + float a1, x1, y1, x2, y2; + sscanf(buf, "%f %f %f %f %f", &a1, &x1, &y1, &x2, &y2); + SCREEN *my_scr = new SCREEN(x1, y1, x2, y2); + my_screen.push_back(my_scr); + printf("Screen was created\n"); + break; + } + case 2: //lens f>0 + { + float a1, x, y, l, deg, f; + sscanf(buf, "%f %f %f %f %f %f",&a1,&x, &y, &l, °, &f); + Device *d = new Lens(x, y, l, deg, f); + my_device.push_back(d); + printf("New lens f>0 was created\n"); + break; + } + case 3: //mirror == PlainRefl + { + float a1, x, y, l, deg; + sscanf(buf, "%f %f %f %f %f",&a1,&x, &y, &l, °); + Device *d = new PlainRefl(x, y, l, deg); + my_device.push_back(d); + printf("New mirror was created\n"); + break; + } + + case 4: //ploskoparallell plastinka == disc + { + float a1, x, y, len, wid, deg, n; + sscanf(buf, "%f %f %f %f %f %f %f",&a1,&x, &y, &len, &wid, °, &n); + Device *d = new Disc(x, y, len, wid, deg, n); + my_device.push_back(d); + printf("New ploskoparallell plastinka was created\n"); + break; + } + case 5: //Laser + { + float a1, x, y, deg; + sscanf(buf, "%f %f %f %f", &a1,&x, &y, °); + my_laser.push_back(new LASER(x,y,deg)); + printf("New laser was created\n"); + break; + } + case 6: //triangle prism + { + float a1, x1, y1, x2, y2, x3, y3, n; + sscanf(buf, "%f %f %f %f %f %f %f %f",&a1,&x1, &y1, &x2, &y2, &x3, &y3, &n); + int num = 1; + Device *d = new Prism(num,x1, y1, x2, y2, x3, y3, n); + my_device.push_back(d); + printf("New triangle prism was created\n"); + break; + } + + case 7: //sphere mirror + { + float a1, x, y, r0, deg_1, deg_2; + sscanf(buf, "%f %f %f %f %f %f",&a1, &x, &y, °_1, °_2, &r0); + Device *d = new SphereRefl(x, y, r0, deg_1-90, deg_2-90); + my_device.push_back(d); + printf("New sphere mirror was created\n"); + break; + } + case 8: //wide length + { + float a1, x, y, l, deg, r1, r2, n, de; + sscanf(buf, "%f %f %f %f %f %f %f %f %f",&a1, &x, &y, &r1, &r2, °, &n, &l, &de); + Device *d = new Lens_wide(x, y, l, deg, r1, r2, n, de); + my_device.push_back(d); + printf("New triangle prism was created\n"); + break; + } + } + + fflush(stdout); + if (strcmp(buf, "FINISH\0")!=0){ + buf[0]=0; + if(send(cs, "1", 1, MSG_NOSIGNAL)==-1) + { + perror("Can't send:"); + return NULL; + } + } + else{ + break; + } + } + + + + point *cross = NULL; + int k = 0; //номер девайса + bool q = false; //true, если пересечения есть + char temp[1]; + vectormy_laser_ray; + for(int i=0; irays_create()); + } +//RAY *my_laser_ray=my_laser->rays_create(); + for(int i=0; irays_create(); + for(int j=0; jcross_point(my_laser_ray[I]); + sprintf(buf_, "%f %f %f %f %c", my_laser_ray[I]->x, my_laser_ray[I]->y, cross->x, cross->y, '\0');//new dot + if(send(cs, buf_, strlen(buf_)+1, MSG_NOSIGNAL)==-1){ + perror("Can't send:"); + return NULL; + } + recv(cs, temp, 1, 0); + float tx=cross->x; + float ty=cross->y; + my_device[num]->change_direction(my_laser_ray[I], cross); + if(my_device[num]->getID()==4){ + sprintf(buf_, "%f %f %f %f %c", tx, ty, my_laser_ray[I]->x, my_laser_ray[I]->y, 0); + if(send(cs, buf_, strlen(buf_)+1, MSG_NOSIGNAL)==-1){ + perror("Can't send:"); + return NULL; + } + recv(cs, temp, 1, 0); + } + } + else{ + break; + } + } + //cross screen + int s_num = first_s(my_screen, my_laser_ray[I]); + if (s_num == -1){ + //find граница, куда дойдет луч + float retx, rety; + retx = my_laser_ray[I]->x + 2000 * cos(GradToRad(my_laser_ray[I]->deg)); + rety = my_laser_ray[I]->y - 2000 * sin(GradToRad(my_laser_ray[I]->deg)); + sprintf(buf_, "%f %f %f %f %c", my_laser_ray[I]->x, my_laser_ray[I]->y, retx, rety, '\0'); + if(send(cs, buf_, strlen(buf_)+1, MSG_NOSIGNAL)==-1) + { + perror("Can't send:"); + return NULL; + } + recv(cs, temp, 1, 0); + + } + else{ + cross = my_screen[s_num]->cross_point(my_laser_ray[I]); + sprintf(buf_, "%f %f %f %f %c", my_laser_ray[I]->x, my_laser_ray[I]->y, cross->x, cross->y, '\0'); + if(send(cs, buf_, strlen(buf_)+1, MSG_NOSIGNAL)==-1) + { + perror("Can't send:"); + return NULL; + } + recv(cs, temp, 1, 0); + } +} + if(send(cs, "FINISH\0", 7, MSG_NOSIGNAL)==-1) + { + perror("Can't send:"); + return NULL; + } + while(recv(cs, buf_, sizeof(buf_)+1, 0)>0); + close(cs); + return NULL; + } diff --git a/051315/main/ray.h b/051315/main/ray.h new file mode 100644 index 0000000..1840228 --- /dev/null +++ b/051315/main/ray.h @@ -0,0 +1,388 @@ +#include +#include +#include "math.h" +#include + +using namespace std; + +#define PI 3.14159265 +#define NUMBER 10 // HERE YOU CAN CHANGE NUMBER OF CREATED RAYS {360 : (NUMBBER - 1)} + + +struct point { + float x ; + float y ; +}; +inline float GradToRad(float deg) { return (float)(deg*PI/180.0);}; +inline float RadToGrad(float arg) { return (float)(arg*180.0/PI);}; +inline float Sqr(float x) {return x*x;} + +int orient_ (float x1, float y1, float x2, float y2, float x3, float y3, float this_deg) {// x1,y1; x2,y2 - line x3,y3 - point + // cout << "x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 << " x3 = " << x1 << " y3 = " << y3 << "\n"; + if (this_deg == 0){ + if (y3 <= y2){ + + // cout << "trying 1 \n"; + return 1; + } + else{ + // cout << "trying -1 \n"; + return -1; + } + } + else { + if (((x3 -x1) * (y2 - y1) - (y3 -y1) * (x2 - x1)) >= 0) + return 1; + else + return -1; + } + +} + +/* +class RAY{ +public: + float x, y; + float deg; + void set_ray_pos(float x_0, float y_0, float deg_0){ + x = x_0; + y = y_0; + deg = deg_0; + } + void show_ray_pos(int i){ + printf("%d: x = %f, y = %f, deg = %f\n", i, x, y, deg); + } + RAY(float x_0, float y_0, float deg_0){ + x = x_0; + y = y_0; + deg = deg_0; + } + ~RAY (){ + printf("Ray was destructed\n"); + } +};*/ + +/*class RAY +{ +public: + float x, y; + float deg; + +public: + void set_ray_pos (float x_0, float y_0, float deg_0) + { x = x_0; y = y_0; deg = deg_0;} + void show_ray_pos(int i) + { cout <y - pp->y), (pp->x - this->x))); + r_deg_delta = Deg360(r_deg_delta); + float r_deg = this->deg; + if (r_deg == 360) r_deg = 0; + if (fabs(r_deg - r_deg_delta) < 1) return true; + return false; + } + + void show_ray_pos(int i) + { cout << "Ray(" << i << "):" << " x = " << x << " y = " << y << " deg = " << deg << "\n";} + + float Deg360(float deg1) const + { // угол в диапазоне [0..360] + do { + if (deg1 < 0) deg1 += 360; + if (deg1 >360) deg1 -= 360; + } + while (deg1 < 0 || deg1 > 360); + return deg1; + } + + ~RAY () + { + cout << "Destructure of the ray (" << deg << ") x: " << x << " y: " << y <<"\n"; + } +}; + + +class SCREEN +{ + public : + float x1, x2, y1, y2,deg; +public: + SCREEN (float x, float y, float l_0, float deg_0) { + x1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180); + x2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180); + y1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180); + y2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180); + deg = deg_0; + } + void screen_pos() + { cout << " x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 <<"\n";} + + point * cross_point (RAY * r) { + point * p = new point (); + if ((orient_ (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))){ + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + if ((orient_ (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + cout << "p->x = " << p->x << "\n" ; + p->y = (float)det_2/det; + cout << "p->x = " << p->x << "\n" ; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } return NULL; + } + + ~SCREEN () + { + cout << "Destructure of the screen" << "\n"; + } +}; + +/*class SCREEN +{ +public : + float x1, x2, y1, y2; + SCREEN (float x1_0, float y1_0, float x2_0, float y2_0 ) : x1 (x1_0), y1 (y1_0), x2 (x2_0), y2 (y2_0) + { } + void screen_pos(){ + cout << " x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 <<"\n"; + } + + point * cross_point (RAY * r){ + point * p = new point (); + if ((( this->x1 + this->x2 )/2 > r->x) && ((r->deg <= 90) || (r->deg >= 270))) { + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + if ((( this->x1 + this->x2 )/2 > r->x) && ((r->deg <= 90) || (r->deg >= 270))) { + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + + return NULL; + } + + ~SCREEN (){ + cout << "Destructure of the screen" << "\n"; + } +}; +*/ +class SOURCE +{ +public: + float x, y; + +public: + SOURCE (float x_0, float y_0) : + x(x_0), y(y_0) + { } + void source_pos() + { cout << " x = " << x << " y = " << y << "\n";} + RAY** rays_create () + { const int num_of_rays = NUMBER; + float deg_step = (float)360 / (num_of_rays - 1); + RAY ** ray = new RAY *[num_of_rays]; + float deg_i = 0; + for (int i = 0; i < num_of_rays; i++) { + ray[i]=new RAY(); + ray[i]->set_ray_pos (x, y, deg_i); + ray[i]->show_ray_pos(i); + deg_i = deg_i + deg_step; + } + return ray; + } + ~SOURCE () + { + cout << "Destructure of the source" << "\n"; + } +}; + + +/* +class Laser{ +public: + RAY *ray; + Laser(float x, float y, float deg){ + ray = new RAY(x, y, deg); + } + ~Laser(){ + printf("Laser destructed\n"); + } +}; +*/ + + + +/*class SCREEN +{ + public : + float x1, x2, y1, y2,deg; +public: + SCREEN (float x, float y, float l_0, float deg_0) { + x1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180); + x2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180); + y1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180); + y2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180); + deg = deg_0; + } + void screen_pos() + { cout << " x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 <<"\n";} + + point * cross_point (RAY * r) { + point * p = new point (); + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))){ + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + cout << "p->x = " << p->x << "\n" ; + p->y = (float)det_2/det; + cout << "p->x = " << p->x << "\n" ; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } return NULL; + } + + ~SCREEN () + { + cout << "Destructure of the screen" << "\n"; + } +}; + +class LASER +{ +private: + float x, y, deg; + +public: + LASER (float x_0, float y_0, float deg_0) : + x(x_0), y(y_0), deg (deg_0) + { } + void laser_pos() + { cout << " x = " << x << " y = " << y << "\n";} + RAY * rays_create () + { + RAY * ray = new RAY (); + ray->set_ray_pos (x, y, deg); + ray->show_ray_pos(0); + return ray; + } + ~LASER () + { + cout << "Destructure of the source" << "\n"; + } +}; +class SCREEN +{ + public : + float x1, x2, y1, y2,deg; +public: + SCREEN (float x, float y, float l_0, float deg_0) { + x1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180); + x2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180); + y1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180); + y2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180); + deg = deg_0; + } + void screen_pos() + { cout << " x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 <<"\n";} + + point * cross_point (RAY * r) { + point * p = new point (); + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))){ + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + cout << "p->x = " << p->x << "\n" ; + p->y = (float)det_2/det; + cout << "p->x = " << p->x << "\n" ; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } return NULL; + } + + ~SCREEN () + { + cout << "Destructure of the screen" << "\n"; + } +}; +*/ +class LASER +{ +private: + float x, y, deg; + +public: + LASER (float x_0, float y_0, float deg_0) : + x(x_0), y(y_0), deg (deg_0) + { } + void laser_pos() + { cout << " x = " << x << " y = " << y << "\n";} + RAY * rays_create () + { + RAY * ray = new RAY (); + ray->set_ray_pos (x, y, deg); + ray->show_ray_pos(0); + return ray; + } + ~LASER () + { + cout << "Destructure of the source" << "\n"; + } +}; + From 0306736ab5dc26b58f8d8296f226bf02dd45c695 Mon Sep 17 00:00:00 2001 From: AntonGerasimov Date: Sun, 17 May 2015 21:00:59 +0300 Subject: [PATCH 08/19] Main folder --- 051315/main/device.h | 143 ----------------------------- 051315/main/ray.h | 209 ------------------------------------------- 2 files changed, 352 deletions(-) diff --git a/051315/main/device.h b/051315/main/device.h index 727f243..709a02e 100644 --- a/051315/main/device.h +++ b/051315/main/device.h @@ -409,149 +409,6 @@ class Disc : public Device{ // n > 1 !!! }; - - - -/* -class Lens_ras : public Device{ -public: - float deg, f, l; - -public: - Lens_ras ( float x, float y, float l_0, float deg_0, float f_0){ //deg from vertical 0 <= deg < 90 !!! against hour ; - x1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180) ; - x2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180); - y1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180); - y2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180); - l = l_0; - f = f_0; - deg = deg_0; - } - point * cross_point (RAY * r) const { - point * p = new point (); - if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))){ - cout << "c_here_1 \n"; - float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); - float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); - float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); - p->x = (float)det_1/det; - p->y = (float)det_2/det; - if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) - return p; - } - if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { - cout << "c_here_2 \n"; - float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); - float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); - float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); - p->x = (float)det_1/det; - p->y = (float)det_2/det; - if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) - return p; - } - return NULL; - } - - void change_direction(RAY * r, point * p ) const - { - float l_1 = 0; - float alpha = 0; - float alpha_r = 0; - float c = 0 ; - float length = (float) sqrt ((r->x - (this->x1 + this->x2)/2) * (r->x - (this->x1 + this->x2)/2)+ (r->y - (this->y1 + this->y2)/2) * (r->y - (this->y1 + this->y2)/2)) ; - float line_tg = fabs ((float)((r->y - (this->y1 + this->y2)/2) / ((this->x1 + this->x2)/2 - r->x))) ; - //cout << " line_tg_prev = " << line_tg << "\n"; - float line_deg = atan (line_tg) * 180 / PI; - cout << "line_deg_prev = " << line_deg <<"\n"; - if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) { - if (r->y <= (this->y1 + this->y2)/2) - line_deg = this->deg + line_deg; - else - line_deg = fabs (line_deg - this->deg); - } - else { - if (r->y >= (this->y1 + this->y2)/2) - line_deg = this->deg + line_deg; - else - line_deg = fabs (line_deg - this->deg); - } - - cout << "line_deg = " << line_deg << "\n"; - line_tg = tan (line_deg * PI / 180); - - //cout << " line_tg = " << line_tg << "\n"; - float a = length * cos (atan (line_tg)); - //cout << " a = " << a << "\n"; - float b = (float) (this->f * a / (a + this->f)); - //cout << " b = " << b << "\n"; - c = sqrt ((p->x - (this->x1 + this->x2)/2) * (p->x - (this->x1 + this->x2)/2)+ (p->y - (this->y1 + this->y2)/2) * (p->y - (this->y1 + this->y2)/2)) ; - - if (((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) || ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0))) { // точка пересечения и координата луча по разные стороны от опт.оси - l_1 = c + (float) b * line_tg; - cout << "here \n"; - alpha = atan ((float) (l_1 / b)) * 180 / PI; - alpha_r = 180 -alpha ; - if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2/cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2/cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) { //точка координата луча выше точка пересечения ниже - alpha_r = alpha_r + 2 * alpha; - alpha = 360 - alpha; - cout << " here_0 \n"; - } - - } - else { //точка персечкения и полоожение луча по одну сторону от опт.оси - l_1 = fabs (c - (float) b * line_tg); - alpha = atan ((float) (l_1 / b)) * 180 / PI; - alpha_r = 180 -alpha ; - cout << "here_here \n"; - if ((orient (this->x1, this->y1 + this->l / 2/cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)){ // луч выше опт.оси точка пересечения под всмогат.линией сверху - alpha_r = alpha_r + 2 * alpha; - alpha = 360 - alpha; - cout << "here_1 \n"; - - } - - - if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) { // луч ниже опт.оси точка пересечения над всмогат.линией снизу - alpha_r = alpha_r + 2 * alpha; - alpha = 360 - alpha; - cout << "alpha =" << alpha << "\n"; - cout << "here_2 \n"; - } - - } - if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) - r->deg = alpha ; - else - r->deg = alpha_r; - - if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) { - if ((r->deg + this->deg - 360) > 0) - r->deg = (r->deg + this->deg - 360); - else - r->deg = r->deg + this->deg; - } - else { - - r->deg = r->deg + this->deg; - } - - r->x = p->x; - r->y = p->y; - } - const char *getID() const{ - return "Lens_ras"; - } - - ~Lens_ras () - { - cout << "Destructure of the Lens_ras" << "\n"; - } - - -}; -*/ - - class Lens : public Device{ public: float deg, f, l; diff --git a/051315/main/ray.h b/051315/main/ray.h index 1840228..39950c1 100644 --- a/051315/main/ray.h +++ b/051315/main/ray.h @@ -38,46 +38,6 @@ int orient_ (float x1, float y1, float x2, float y2, float x3, float y3, float t } } - -/* -class RAY{ -public: - float x, y; - float deg; - void set_ray_pos(float x_0, float y_0, float deg_0){ - x = x_0; - y = y_0; - deg = deg_0; - } - void show_ray_pos(int i){ - printf("%d: x = %f, y = %f, deg = %f\n", i, x, y, deg); - } - RAY(float x_0, float y_0, float deg_0){ - x = x_0; - y = y_0; - deg = deg_0; - } - ~RAY (){ - printf("Ray was destructed\n"); - } -};*/ - -/*class RAY -{ -public: - float x, y; - float deg; - -public: - void set_ray_pos (float x_0, float y_0, float deg_0) - { x = x_0; y = y_0; deg = deg_0;} - void show_ray_pos(int i) - { cout <x1 + this->x2 )/2 > r->x) && ((r->deg <= 90) || (r->deg >= 270))) { - float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); - float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); - float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); - p->x = (float)det_1/det; - p->y = (float)det_2/det; - if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) - return p; - } - if ((( this->x1 + this->x2 )/2 > r->x) && ((r->deg <= 90) || (r->deg >= 270))) { - float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); - float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); - float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); - p->x = (float)det_1/det; - p->y = (float)det_2/det; - if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) - return p; - } - - return NULL; - } - - ~SCREEN (){ - cout << "Destructure of the screen" << "\n"; - } -}; -*/ class SOURCE { public: @@ -232,136 +153,6 @@ class SOURCE cout << "Destructure of the source" << "\n"; } }; - - -/* -class Laser{ -public: - RAY *ray; - Laser(float x, float y, float deg){ - ray = new RAY(x, y, deg); - } - ~Laser(){ - printf("Laser destructed\n"); - } -}; -*/ - - - -/*class SCREEN -{ - public : - float x1, x2, y1, y2,deg; -public: - SCREEN (float x, float y, float l_0, float deg_0) { - x1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180); - x2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180); - y1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180); - y2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180); - deg = deg_0; - } - void screen_pos() - { cout << " x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 <<"\n";} - - point * cross_point (RAY * r) { - point * p = new point (); - if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))){ - float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); - float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); - float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); - p->x = (float)det_1/det; - p->y = (float)det_2/det; - if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) - return p; - } - if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { - float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); - float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); - float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); - p->x = (float)det_1/det; - cout << "p->x = " << p->x << "\n" ; - p->y = (float)det_2/det; - cout << "p->x = " << p->x << "\n" ; - if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) - return p; - } return NULL; - } - - ~SCREEN () - { - cout << "Destructure of the screen" << "\n"; - } -}; - -class LASER -{ -private: - float x, y, deg; - -public: - LASER (float x_0, float y_0, float deg_0) : - x(x_0), y(y_0), deg (deg_0) - { } - void laser_pos() - { cout << " x = " << x << " y = " << y << "\n";} - RAY * rays_create () - { - RAY * ray = new RAY (); - ray->set_ray_pos (x, y, deg); - ray->show_ray_pos(0); - return ray; - } - ~LASER () - { - cout << "Destructure of the source" << "\n"; - } -}; -class SCREEN -{ - public : - float x1, x2, y1, y2,deg; -public: - SCREEN (float x, float y, float l_0, float deg_0) { - x1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180); - x2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180); - y1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180); - y2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180); - deg = deg_0; - } - void screen_pos() - { cout << " x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 <<"\n";} - - point * cross_point (RAY * r) { - point * p = new point (); - if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))){ - float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); - float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); - float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); - p->x = (float)det_1/det; - p->y = (float)det_2/det; - if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) - return p; - } - if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { - float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); - float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); - float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); - p->x = (float)det_1/det; - cout << "p->x = " << p->x << "\n" ; - p->y = (float)det_2/det; - cout << "p->x = " << p->x << "\n" ; - if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) - return p; - } return NULL; - } - - ~SCREEN () - { - cout << "Destructure of the screen" << "\n"; - } -}; -*/ class LASER { private: From 5588f02b9eb8381e4c9f2f282952133bdd0a0f4e Mon Sep 17 00:00:00 2001 From: AntonGerasimov Date: Sun, 17 May 2015 21:10:47 +0300 Subject: [PATCH 09/19] Update main.cc --- 051315/main/main.cc | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/051315/main/main.cc b/051315/main/main.cc index a3290a0..cdf82cc 100644 --- a/051315/main/main.cc +++ b/051315/main/main.cc @@ -62,10 +62,10 @@ int main() void *func(void* arg) { -int rd; -char buf[512]; -int cs=*(int *)arg; - char buf_[32]; + int rd; + char buf[512]; + int cs=*(int *)arg; + char buf_[512]; vector my_device; vector my_screen; @@ -243,12 +243,16 @@ for(int I=0; Ix; + ty=my_laser_ray[I]->y; + tdeg=my_laser_ray[I]->deg; int s_num = first_s(my_screen, my_laser_ray[I]); if (s_num == -1){ //find граница, куда дойдет луч float retx, rety; - retx = my_laser_ray[I]->x + 2000 * cos(GradToRad(my_laser_ray[I]->deg)); - rety = my_laser_ray[I]->y - 2000 * sin(GradToRad(my_laser_ray[I]->deg)); + retx = my_laser_ray[I]->x + 2000 * cos(GradToRad(tdeg)); + rety = my_laser_ray[I]->y - 2000 * sin(GradToRad(tdeg)); sprintf(buf_, "%f %f %f %f %c", my_laser_ray[I]->x, my_laser_ray[I]->y, retx, rety, '\0'); if(send(cs, buf_, strlen(buf_)+1, MSG_NOSIGNAL)==-1) { @@ -259,8 +263,9 @@ for(int I=0; Icross_point(my_laser_ray[I]); - sprintf(buf_, "%f %f %f %f %c", my_laser_ray[I]->x, my_laser_ray[I]->y, cross->x, cross->y, '\0'); + sprintf(buf_, "%f %f %f %f %c", tx, ty, crossing->x, crossing->y, '\0'); if(send(cs, buf_, strlen(buf_)+1, MSG_NOSIGNAL)==-1) { perror("Can't send:"); From 1a973a96a3e124bca37c8114705a8e03a1114da3 Mon Sep 17 00:00:00 2001 From: AntonGerasimov Date: Sun, 17 May 2015 21:21:31 +0300 Subject: [PATCH 10/19] forced --- 051315/first/first.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/051315/first/first.h b/051315/first/first.h index e1a0d14..b7e54ff 100644 --- a/051315/first/first.h +++ b/051315/first/first.h @@ -24,6 +24,9 @@ int first(vector d, RAY *r){ float y = r->y; float rast; float min = 100000; + if (d.size==0){ + return ret; + } for (int i = 0; i < d.size(); i++){ point *cross = NULL; cross = d[i] -> cross_point(r); From c2c23d5d01ac15bc040ea880f3f254ff6eba0812 Mon Sep 17 00:00:00 2001 From: AntonGerasimov Date: Sun, 17 May 2015 21:28:40 +0300 Subject: [PATCH 11/19] Ver. 051715 --- device.h | 573 ++++++++++++++++++++++++++++++++++++++++++++----------- first.h | 71 +++++++ main.cc | 230 +++++++++++----------- ray.h | 349 +++++++++++++++++++++++++++++++-- 4 files changed, 985 insertions(+), 238 deletions(-) create mode 100644 first.h diff --git a/device.h b/device.h index 62bfbeb..f886042 100644 --- a/device.h +++ b/device.h @@ -1,16 +1,34 @@ #include #include #include "math.h" +#include "ray.h" using namespace std; #define PI 3.14159265 -#define NUMBER 10 // HERE YOU CAN CHANGE NUMBER OF CREATED RAYS {360 : (NUMBBER - 1)} -struct point { - float x ; - float y ; -}; +float LenVect(point *p1, point *p2) { + return sqrt(Sqr(p2->x - p1->x) + Sqr(p2->y - p1->y)); +} + +float Scalar(point *p1, point *p2) { + return p1->x * p2->x + p1->y * p2->y; +} + +point *SumVect(point *p1, point *p2) { + point *sum_p = new point; + sum_p->x = p1->x + p2->x; + sum_p->y = p1->y + p2->y; + return sum_p; +} + +point *SubVect(point *p1, point *p2) { + point *sub_p = new point; + sub_p->x = p2->x - p1->x; + sub_p->y = p2->y - p1->y; + return sub_p; +} +class Prism; int orient (float x1, float y1, float x2, float y2, float x3, float y3, float this_deg) {// x1,y1; x2,y2 - line x3,y3 - point // cout << "x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 << " x3 = " << x1 << " y3 = " << y3 << "\n"; @@ -31,27 +49,8 @@ int orient (float x1, float y1, float x2, float y2, float x3, float y3, float th else return -1; } - } -class RAY -{ -public: - float x, y; - float deg; - -public: - void set_ray_pos (float x_0, float y_0, float deg_0) - { x = x_0; y = y_0; deg = deg_0;} - void show_ray_pos(int i) - { cout < 1 !!! p_y = p->y; p->x = p_x * cos (this->deg * PI / 180) + p_y * sin (this->deg * PI / 180); p->y = - p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); + r_x = r->x; r_y = r->y; r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); - r->y = -r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + r->y = - r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + r->deg = deg + this->deg; + if (r->deg > 360) + r->deg = r->deg - 360; + return p; } } @@ -168,10 +172,15 @@ class Disc : public Device{ // n > 1 !!! p_y = p->y; p->x = p_x * cos (this->deg * PI / 180) + p_y * sin (this->deg * PI / 180); p->y = - p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); + r_x = r->x; r_y = r->y; r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); - r->y = -r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + r->y = - r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + r->deg = deg + this->deg; + if (r->deg > 360) + r->deg = r->deg - 360; + return p; } } @@ -197,10 +206,15 @@ class Disc : public Device{ // n > 1 !!! p_y = p->y; p->x = p_x * cos (this->deg * PI / 180) + p_y * sin (this->deg * PI / 180); p->y = - p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); + r_x = r->x; r_y = r->y; r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); - r->y = -r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + r->y = - r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + r->deg = deg + this->deg; + if (r->deg > 360) + r->deg = r->deg - 360; + return p; } } @@ -221,30 +235,47 @@ class Disc : public Device{ // n > 1 !!! p_y = p->y; p->x = p_x * cos (this->deg * PI / 180) + p_y * sin (this->deg * PI / 180); p->y = - p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); + r_x = r->x; r_y = r->y; r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); - r->y = -r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + r->y = - r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + r->deg = deg + this->deg; + if (r->deg > 360) + r->deg = r->deg - 360; + return p; } } - + r_x = r->x; + r_y = r->y; + r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); + r->y = - r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + r->deg = deg + this->deg; + if (r->deg > 360) + r->deg = r->deg - 360; return NULL; } void change_direction(RAY * r, point * p ) const { - float p_x, p_y, r_x,r_y; + //cout<< "this->n = " << this->n << "\n"; + float p_x, p_y, r_x, r_y; p_x = p->x; p_y = p->y; p->x = p_x * cos (this->deg * PI / 180) - p_y * sin (this->deg * PI / 180); p->y = p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); + r_x = r->x; r_y = r->y; r->x = r_x * cos (this->deg * PI / 180) - r_y * sin (this->deg * PI / 180); r->y = r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + if (r->deg >= this->deg) + r->deg = r->deg - this->deg; + else + r->deg = 360 + r->deg -this->deg; point * new_p = p; float deg = 0; @@ -342,6 +373,7 @@ class Disc : public Device{ // n > 1 !!! new_p->x = p->x - this->l * tan ( beta * PI / 180); } else { + //cout << beta ; beta = fabs (beta); new_p->y = p->y - this->l; new_p->x = p->x + this->l * tan ( beta * PI / 180); @@ -392,7 +424,7 @@ class Disc : public Device{ // n > 1 !!! } } } - //cout << "beta = " << beta << "\n"; + // cout << "beta = " << beta << "\n"; r->x = new_p->x * cos (this->deg * PI / 180) + new_p->y * sin (this->deg * PI / 180) ; r->y = new_p->y * cos (this->deg * PI / 180) - new_p->x * sin (this->deg * PI / 180); r->deg = deg + this->deg; @@ -412,6 +444,8 @@ class Disc : public Device{ // n > 1 !!! + +/* class Lens_ras : public Device{ public: float deg, f, l; @@ -548,7 +582,7 @@ class Lens_ras : public Device{ }; - +*/ class Lens : public Device{ @@ -838,9 +872,9 @@ class Lens_wide : public Device{ //cout << " a = " << a << "\n"; float f = 0; if (orient (this->x1, this->y1, this->x2, this->y2, r->x, r->y,1) < 0) - f = (float) 1 / ((this->n -1) * ((float)1/this->r1 - (float)1/this->r2 + (float) ((this->n -1) * this->d) / (this->n * this->r1 *this->r2))) ; + f = (float) 1 / ((this->n -1) * ((float)1/this->r1 - (float)1/this->r2 + (float) ((this->n-1) * this->d) / (this->n * this->r1 *this->r2))) ; else - f = (float) 1 / ((this->n -1) * ((float)1/this->r2 - (float)1/this->r1 + (float) ((this->n - 1)* this->d) / (this->n * this->r1 *this->r2))) ; + f = (float) 1 / ((this->n -1) * ((float)1/this->r2 - (float)1/this->r1 + (float) ((this->n-1) * this->d) / (this->n * this->r1 *this->r2))) ; if (a != this->f){ if (a > f) { float b = (float) (f * a / (a - f)); @@ -982,108 +1016,427 @@ class Lens_wide : public Device{ cout << "Destructure of the Lens" << "\n"; } }; +//1) Плоское зеркало (XY – координаты; lenght – длина зеркала; deg_0 – угол поворота). +class PlainRefl : public Device { + public: + float deg, f; - - - -class SCREEN -{ - public : - float x1, x2, y1, y2,deg; public: - SCREEN (float x, float y, float l_0, float deg_0) { - x1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180); - x2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180); - y1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180); - y2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180); + PlainRefl( float x, float y, float l, float deg_0){ //deg from vertical 0 <= deg <= 90 against hour ; + x1 = x - (float)(l/2) * sin (GradToRad(deg_0)) ; + x2 = x + (float)(l/2) * sin (GradToRad(deg_0)); + y1 = y - (float)(l/2) * cos (GradToRad(deg_0)); + y2 = y + (float)(l/2) * cos (GradToRad(deg_0)); deg = deg_0; + std::cout << "Create PlainRefl (" << x1 << ", " << y1 << "), (" << x2 << ", " << y2 << ") deg: " << deg << "\n"; } - void screen_pos() - { cout << " x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 <<"\n";} - - point * cross_point (RAY * r) { - point * p = new point (); - if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))){ - float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); - float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); - float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + point *cross_point(RAY *r) const { + point *p = new point(); + if ((( this->x1 + this->x2 )/2 > r->x) && ((r->deg <= 90) || (r->deg > 270))){ + float det = this->y2 - this->y1 - tan (GradToRad(r->deg)) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 + - this->y1 * this->x2 + - (this->x1 - this->x2) * (r->y + tan (GradToRad(r->deg)) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (GradToRad(r->deg)) * r->x) + - (this->y2 * this->x1 - this->y1 * this->x2) * tan (GradToRad(r->deg)); p->x = (float)det_1/det; p->y = (float)det_2/det; - if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) - return p; + if ((( p->x >= x1 ) && ( p->x <= x2 ) && (x1 <= x2)) && (( p->y >= y1 ) && ( p->y <= y2 ) && (y1 <= y2))) return p; + if ((( p->x <= x1 ) && ( p->x >= x2 ) && (x1 >= x2)) && (( p->y >= y1 ) && ( p->y <= y2 ) && (y1 <= y2))) return p; + if ((( p->x >= x1 ) && ( p->x <= x2 ) && (x1 <= x2)) && (( p->y <= y1 ) && ( p->y >= y2 ) && (y1 >= y2))) return p; + if ((( p->x <= x1 ) && ( p->x >= x2 ) && (x1 >= x2)) && (( p->y <= y1 ) && ( p->y >= y2 ) && (y1 >= y2))) return p; } - if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { - float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); - float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); - float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + if ((( this->x1 + this->x2 )/2 < r->x) && ((r->deg >= 90) && (r->deg <= 270))) { + float det = this->y2 - this->y1 - tan (GradToRad(r->deg)) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (GradToRad(r->deg)) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (GradToRad(r->deg)) * r->x) + - (this->y2 * this->x1 - this->y1 * this->x2) * tan (GradToRad(r->deg)); p->x = (float)det_1/det; - cout << "p->x = " << p->x << "\n" ; p->y = (float)det_2/det; - cout << "p->x = " << p->x << "\n" ; - if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) - return p; - } return NULL; + if ((( p->x >= x1 ) && ( p->x <= x2 ) && (x1 <= x2)) && (( p->y >= y1 ) && ( p->y <= y2 ) && (y1 <= y2))) return p; + if ((( p->x <= x1 ) && ( p->x >= x2 ) && (x1 >= x2)) && (( p->y >= y1 ) && ( p->y <= y2 ) && (y1 <= y2))) return p; + if ((( p->x >= x1 ) && ( p->x <= x2 ) && (x1 <= x2)) && (( p->y <= y1 ) && ( p->y >= y2 ) && (y1 >= y2))) return p; + if ((( p->x <= x1 ) && ( p->x >= x2 ) && (x1 >= x2)) && (( p->y <= y1 ) && ( p->y >= y2 ) && (y1 >= y2))) return p; + } + return NULL; } - - ~SCREEN () + + void change_direction(RAY *r, point *p) const // Плоское зеркало { - cout << "Destructure of the screen" << "\n"; + r->x = p->x; + r->y = p->y; + r->deg = (90 + this->deg) * 2 - r->deg; + + r->deg = r->Deg360(r->deg); + } + const char *getID() const { + return "PlainRefl"; + } + + ~PlainRefl () + { + cout << "Destructure of the PlainRefl" << "\n"; } }; -class LASER -{ -private: - float x, y, deg; - -public: - LASER (float x_0, float y_0, float deg_0) : - x(x_0), y(y_0), deg (deg_0) - { } - void laser_pos() - { cout << " x = " << x << " y = " << y << "\n";} - RAY * rays_create () + +//2) Сферическое зеркало ( +// XY – координаты; +// lenght – длина зеркала; +// R – радиус кривизны; +// deg_1, deg_2 – углы, под которыми видны нижняя и верхняя границы зеркала). + +class SphereRefl : public Device { + public: + float deg1, deg2, R; + point *OptC; + SphereRefl( float x, float y, float R0, float deg_1, float deg_2) + { + OptC = new point(); //deg from vertical 0 <= deg <= 90 against hour ; + OptC->x = x; + OptC->y = y; + R = R0; // Радиус кривизны сферы + x1 = OptC->x - R * sin (GradToRad(deg_1)) ; + x2 = OptC->x - R * sin (GradToRad(deg_2)); + y1 = OptC->y - R * cos (GradToRad(deg_1)); + y2 = OptC->y - R * cos (GradToRad(deg_2)); + this->f = fabs(this->R); // фокусное расстояние + deg1 = deg_1; + deg2 = deg_2; + std::cout << "\nCreate SphereRefl x: " << x << " y: " << y << " R: " << R << " deg1:" << deg1 << " deg2: " << deg2 << "\n"; + std::cout << "X1:" << x1 << " Y1: " << y1 << " X2: " << x2 << " Y2: " << y2 << "\n"; + } + + point *cross_point(RAY *r) const { // точка пересечения сферы и луча + point *p = new point(); + + float xn, yn, k_ray, b_ray; + float xc, yc; + if (r->deg != 0) { + xn = r->y / tan (GradToRad(r->deg)) + r->x; + yn = r->x * tan (GradToRad(r->deg)) + r->y; + + k_ray = -yn/xn; // уравнение луча + b_ray = yn; + // пересечение луча и нормали, проходящей через центр сферы + xc = (OptC->y - OptC->x/k_ray - yn) / (k_ray + 1/k_ray); // k_ray = 0 ? + yc = -(xc + OptC->x)/ k_ray + OptC->y; + } else { + xc = OptC->x; + yc = r->y; + } + point pc; + pc.x = xc; + pc.y = yc; + point p_ray; + p_ray.x = r->x; + p_ray.y = r->y; + float l_ray_optc = LenVect(&p_ray, OptC); + float l_ray_pc = LenVect(&p_ray, &pc); + float l_d = sqrt(l_ray_optc * l_ray_optc - l_ray_pc * l_ray_pc); + float l_ray; + if (this->R >= l_d) { + l_ray = fabs(l_ray_pc - sqrt(R * R - l_d * l_d)); + p->x = r->x + l_ray * cos(GradToRad(r->deg)); + p->y = r->y - l_ray * sin(GradToRad(r->deg)); + + float delta = Sqr(p->x - OptC->x) + Sqr(p->y - OptC->y) - Sqr(R); + std::cout << "1) p->x: " << p->x << " p->y: " << p->y << " r->deg: " << r->deg << " delta:" << delta << "\n"; + // Проверка принадлежности сегменту + // Находим угол от вертикали + float deg_t; + if (fabs(delta) < 0.1) { + deg_t = RadToGrad(atan((OptC->x - p->x) / (OptC->y - p->y))); + // if (OptC->x - p->x<0)deg_t +=180; + deg_t = r->Deg360(deg_t); + + std::cout << "Sphere Point 1: x=" << p->x << " y=" << p->y << " Sphere Deg=" << deg_t << " deg1=" << deg1 << " deg2=" << deg2 << "\n"; + if (deg1 <= deg_t && deg_t <= deg2 ) return p; + } + + // Второй корень + l_ray = fabs(l_ray_pc + sqrt(R * R - l_d * l_d)); + p->x = r->x + l_ray * cos(GradToRad(r->deg)); + p->y = r->y - l_ray * sin(GradToRad(r->deg)); + + delta = Sqr(p->x - OptC->x) + Sqr(p->y - OptC->y) - Sqr(R); + std::cout << "2) p->x: " << p->x << " p->y: " << p->y << " r->deg: " << r->deg << " delta:" << delta << "\n"; + + if (fabs(delta) < 0.1) { + deg_t = RadToGrad(atan((OptC->x - p->x) / (OptC->y - p->y))); + deg_t = r->Deg360(deg_t); + + std::cout << "Sphere Point 2: x=" << p->x << " y=" << p->y << " Sphere Deg=" << deg_t << " deg1=" << deg1 << " deg2=" << deg2 << "\n"; + if (deg1 <= deg_t && deg_t <= deg2 ) return p; + } + } + + return NULL; + } + + void change_direction(RAY *r, point *p) const // Сферическое зеркало { - RAY * ray = new RAY (); - ray->set_ray_pos (x, y, deg); - ray->show_ray_pos(0); - return ray; + std::cout << "\nPrev r->deg: " << r->deg << "\n"; + float a1 = p->x - r->x; + float b1 = r->y - p->y; + float a2 = p->x - OptC->x; + float b2 = OptC->y - p->y; + float deg_f = RadToGrad(atan((a1 * b2 - a2 * b1) / (a1 * a2 + b1 * b2))); // угол между лучом и нормалью к сфере + std::cout << "Deg ray-normal: " << deg_f << "\n"; + + + r->x = p->x; + r->y = p->y; + r->deg = r->deg + (180 - 2 * fabs(deg_f)); + r->deg = r->Deg360(r->deg); + std::cout << "New r->deg: " << r->deg << "\n"; + } + + const char *getID() const { + return "SphereRefl"; } - ~LASER () + + ~SphereRefl() { - cout << "Destructure of the source" << "\n"; + cout << "Destructure of the " << this->getID() << "\n"; } }; +class Gran { +public: + Prism *prism; + point *p1,*p2; + point *normal; + Gran(Prism *_prism, point *_p1, point *_p2) + { + prism = _prism; + p1 = new point; + p2 = new point; + p1->x = _p1->x; + p1->y = _p1->y; + p2->x = _p2->x; + p2->y = _p2->y; + normal = new point(); + normal->x = (p2->y - p1->y); + normal->y = (p1->x - p2->x); + } + float CheckPointCosA (point *pp) const // точка pp внутри [p1,p2] + { + return Scalar(SubVect(p1, pp), SubVect(p2, pp)) / (LenVect(pp, p1) * LenVect(pp, p2)); + } + bool CheckPoint (point *pp) const // точка pp внутри [p1,p2] + { + float cos_a = Scalar(SubVect(p1, pp), SubVect(p2, pp)) / (LenVect(pp, p1) * LenVect(pp, p2)); + if (cos_a < 0) return true; + return false; + } + point *cross_point(RAY *r) const + { // точка пересечения грани призмы и луча + point *p = new point(); + float p_check = (r->x - p1->x) * (p2->y - p1->y) - (r->y - p1->y) * (p2->x - p1->x); // источник луча находится на грани призмы + if (fabs(p_check) < 0.1) return NULL; + float x1, y1, x2, y2, r1, r2; + float det, det1, det2; + float xp, yp; -class SOURCE -{ -private: - float x, y; - + if (r->deg != 90 && r->deg != 270) { // tan 90 + x1 = p2->y - p1->y; + y1 = -(p2->x - p1->x); + r1 = p1->x * x1 + p1->y * y1; + + x2 = tan(GradToRad(r->deg)); + y2 = 1; + r2 = r->x * x2 + r->y; + + det = x1 * y2 - x2 * y1; // определитель - метод Крамера + det1 = r1 * y2 - r2 * y1; + det2 = x1 * r2 - x2 * r1; + + if (det == 0) return NULL; // прямые параллельны + p->x = det1 / det; + p->y = det2 / det; + } + else // луч параллелен оси y + { + x1 = p2->y - p1->y; + y1 = -(p2->x - p1->x); + r1 = p1->x * x1 + p1->y * y1; + + p->x = r->x; + p->y = (r1 - r->x * x1) / y1; + } + if (CheckPoint (p)) return p; + + + return NULL; + } +}; +//5) Призма (n - № объекта; XY– координаты; n – показатель преломления). +class Prism : public Device { public: - SOURCE (float x_0, float y_0) : - x(x_0), y(y_0) - { } - void source_pos() - { cout << " x = " << x << " y = " << y << "\n";} - RAY** rays_create () - { const int num_of_rays = NUMBER; - float deg_step = (float)360 / (num_of_rays - 1); - RAY ** ray = new RAY *[num_of_rays]; - float deg_i = 0; - for (int i = 0; i < num_of_rays; i++) { - ray[i]=new RAY(); - ray[i]->set_ray_pos (x, y, deg_i); - ray[i]->show_ray_pos(i); - deg_i = deg_i + deg_step; + int N; + float len, p, a , b, n_coeff; + vector gran_vect; + vector P; + float deg1, deg2; + point *OC; + + Prism(int _N, float _x1, float _y1, float _x2, float _y2, float _x3, float _y3, float _n_coeff) + { + N = _N; + n_coeff = _n_coeff; + Gran *gran_t; + point *p_t = new point; + p_t->x = _x1; + p_t->y = _y1; + P.push_back(p_t); + p_t = new point; + p_t->x = _x2; + p_t->y = _y2; + P.push_back(p_t); + p_t = new point; + p_t->x = _x3; + p_t->y = _y3; + P.push_back(p_t); + gran_t = new Gran((Prism *)this, P[0], P[1]); + gran_vect.push_back(gran_t); + gran_t = new Gran((Prism *)this, P[1], P[2]); + gran_vect.push_back(gran_t); + gran_t = new Gran((Prism *)this, P[2], P[0]); + gran_vect.push_back(gran_t); + + std::cout << "\n Create Prism N: " << N << "\n"; + for (unsigned i = 0; i < P.size();i++) { + std::cout << "Point " << i << " x: " << P[i]->x << " y: " << P[i]->y << "\n"; + } + std::cout << "Coeff_N: " << n_coeff << "\n"; + } + + point *cross_point(RAY *r) const { // точка пересечения призмы и луча + point *p = NULL; + float len_min = 1e10; + + for (unsigned i = 0; i < gran_vect.size(); i++) { + point *p_t = gran_vect[i]->cross_point(r); // проверка грани призмы и луча + if (p_t != NULL) { + if (r->CheckRayPoint(p_t)) { + point *pr = new point(); + pr->x = r->x; + pr->y = r->y; + float len_t = LenVect(p_t, pr); // минимальное расстояние до источника + if (len_min > len_t) { + len_min = len_t; + p = new point(); + p->x = p_t->x; + p->y = p_t->y; + } + } + } + } + if (p != NULL) { + std::cout << "Prism intersection \n"; + std::cout << "p->x: " << p->x << " p->y: " << p->y << " r->deg: " << r->deg << "\n"; + return p; + } + + return NULL; + } + + Gran *FindGran(point *pp) const + { + float cos_a = 0; + for (unsigned i=0; i < gran_vect.size(); i++) { // нашли грань для точки p + cos_a = gran_vect[i]->CheckPointCosA(pp); + if (fabs(cos_a + 1) < 0.1) + { + return gran_vect[i]; + break; + } + } + return NULL; } - return ray; + + void change_direction(RAY *r, point *p) const // Призма + { + std::cout << "\nPrev r->deg: " << r->deg << "\n"; // исходный угол луча + std::cout << "Point_in: x: " << p->x << " y: " << p->y << " \n"; + std::cout << "Coeff n : " << n_coeff << " \n"; + float cos_a = 0; + Gran *gran = FindGran(p); + + point *r0 = new point(); + r0->x = r->x; + r0->y = r->y; + + cos_a = Scalar(SubVect(p, r0), gran->normal) / (LenVect(p, r0) * sqrt(Sqr(gran->normal->x) + Sqr(gran->normal->y))); + float deg_f = RadToGrad(acos(cos_a)); + std::cout << "Deg_in: " << deg_f << "\n"; + while (fabs(deg_f) > 90 ) { + deg_f = 180 - deg_f; + std::cout << "Deg_in: " << deg_f << " 180-a \n"; + } + float deg_r = RadToGrad(asin(sin(GradToRad(deg_f)) / n_coeff)); + std::cout << "Deg_r_in: " << deg_r << "\n"; + while (fabs(deg_r) > 90 ) { + deg_r = 180 - deg_r; + std::cout << "Deg_r_in: " << deg_r << " 180-a \n"; + } + + float deg_r_new = r->deg - deg_f + deg_r; + + std::cout << "Deg_ray_new " << deg_r_new << "\n"; + + RAY *ray_in = new RAY; + ray_in->set_ray_pos (p->x, p->y, ray_in->Deg360(deg_r_new)); + + point *p_new = cross_point(ray_in); // выходная точка + if (p_new != NULL) + { + Gran *gran_out = FindGran(p_new); + std::cout << "\n Point_out: x: " << p_new->x << " y: " << p_new->y << "\n"; + + cos_a = Scalar(SubVect(p_new, r0), gran_out->normal) / (LenVect(p_new, r0) * sqrt(Sqr(gran_out->normal->x) + Sqr(gran_out->normal->y))); + float deg_r_out = RadToGrad(acos(cos_a)); // угол между лучом и выходной нормалью + std::cout << " Deg_r_out: " << deg_r_out << "\n"; + while (fabs(deg_r_out) > 90 ) { + deg_r_out = 180 - deg_r_out; // 0..90 + std::cout << " Deg_r_out: " << deg_r_out << " 180-a \n"; + } + if (sin(GradToRad(deg_r_out)) * n_coeff > 1) { + std::cout << " sin(Deg_i_out) * n > 1: " << sin(GradToRad(deg_r_out)) * n_coeff << "\n"; + deg_r_out = 90 - deg_r_out; + } + float deg_i_out = RadToGrad(asin(sin(GradToRad(deg_r_out)) * n_coeff)); // sin(i_out) = sin(r_out) * n + std::cout << " Deg_i_out: " << deg_i_out << "\n"; + while (fabs(deg_i_out) > 90 ) { + deg_i_out = 180 - deg_i_out; // 0..90 + std::cout << " Deg_i_out: " << deg_i_out << " 180-a \n"; + } + + float deg_i_out_new = ray_in->deg - deg_i_out + deg_r_out; + std::cout << "Deg_i_out_new: " << deg_i_out_new << "\n"; + + r->deg = r->Deg360(deg_i_out_new); + r->x = p_new->x; + r->y = p_new->y; + } + r->x = p->x; + r->y = p->y; + r->deg = r->Deg360(r->deg); // угол луча в [0..360] + std::cout << "New r->deg: " << r->deg << "\n"; + } + + + const char *getID() const { + return "Prism"; } - ~SOURCE () + + ~Prism() { - cout << "Destructure of the source" << "\n"; + cout << "Destructure of the " << this->getID() << "\n"; } }; + + diff --git a/first.h b/first.h new file mode 100644 index 0000000..b7e54ff --- /dev/null +++ b/first.h @@ -0,0 +1,71 @@ +#include +#include "device.h" +#include + + +float min_(float x1, float x2){ + if (x1 < x2) + return x1; + else return x2; +} +void print_(vector my_device){ + for (int i = 0; i < my_device.size(); i++){ + printf("%d:%f\n", i,min_(my_device[i]->x1, my_device[i]->x2)); + } + printf("\n"); +} +float sqr_(float x){ + return x*x; +} + +int first(vector d, RAY *r){ + int ret = -1; + float x = r->x; + float y = r->y; + float rast; + float min = 100000; + if (d.size==0){ + return ret; + } + for (int i = 0; i < d.size(); i++){ + point *cross = NULL; + cross = d[i] -> cross_point(r); + if (cross != NULL){ + float xp = cross -> x; + float yp = cross -> y; + rast = sqrt(sqr_(x - xp)+sqr_(y - yp)); + } + else{ + rast = 1000000; + } + if (rast < min){ + min = rast; + ret = i; + } + } + return ret; +} +int first_s(vector d, RAY *r){ + int ret = -1; + float x = r->x; + float y = r->y; + float rast; + float min = 100000; + for (int i = 0; i < d.size(); i++){ + point *cross = NULL; + cross = d[i] -> cross_point(r); + if (cross != NULL){ + float xp = cross -> x; + float yp = cross -> y; + rast = sqrt(sqr_(x - xp)+sqr_(y - yp)); + } + else{ + rast = 1000000; + } + if (rast < min){ + min = rast; + ret = i; + } + } + return ret; +} diff --git a/main.cc b/main.cc index 382fd1e..cdf82cc 100644 --- a/main.cc +++ b/main.cc @@ -7,10 +7,10 @@ #include #include //#include "ray.h" //was included in "device.h" -//#include "device.h" //was included in "sort.h" -#include "./sort.h" #include #include +#include "first.h" + int h; @@ -62,13 +62,13 @@ int main() void *func(void* arg) { -int rd; -char buf[512]; -int cs=*(int *)arg; - char buf_[32]; + int rd; + char buf[512]; + int cs=*(int *)arg; + char buf_[512]; vector my_device; - SCREEN *my_screen; + vector my_screen; vector my_laser; vector my_source; if(send(cs, "1", 1, MSG_NOSIGNAL)==-1) @@ -79,23 +79,34 @@ int cs=*(int *)arg; while((rd=recv(cs, buf, sizeof(buf), 0))>0){ buf[rd]=0; printf("%s\n", buf); - int check = buf[0] - '0'; - printf("check = %d\n", check); + int check; + + if ((buf[1]>='0')&&(buf[1]<='9')){ + int check_10 = buf[0] - '0'; + check_10 = check_10 * 10; + int check_1 = buf[1] - '0'; + check = check_10 + check_1; + printf("check = %d\n", check); + } + else{ + check = buf[0] - '0'; + } switch(check){ - case 0: - { - float a1,x,y; - sscanf(buf, "%f %f %f", &a1, &x, &y); - SOURCE* d=new SOURCE(x,y); - my_source.push_back(d); - printf("New source was created\n"); - break; - } + case 0: //source + { + float a1,x,y; + sscanf(buf, "%f %f %f", &a1, &x, &y); + SOURCE* d=new SOURCE(x,y); + my_source.push_back(d); + printf("New source was created\n"); + break; + } case 1: //screen { float a1, x1, y1, x2, y2; sscanf(buf, "%f %f %f %f %f", &a1, &x1, &y1, &x2, &y2); - my_screen = new SCREEN (x1,y1, x2,y2); + SCREEN *my_scr = new SCREEN(x1, y1, x2, y2); + my_screen.push_back(my_scr); printf("Screen was created\n"); break; } @@ -108,22 +119,21 @@ int cs=*(int *)arg; printf("New lens f>0 was created\n"); break; } -/* case 3: //lens f<0 - { - float a1, x, y, l, deg, f; - sscanf(buf, "%f %f %f %f %f %f",&a1,&x, &y, &l, °, &f); - printf("%lf", f); - Device *d = new Lens(x, y, l, deg, f); + case 3: //mirror == PlainRefl + { + float a1, x, y, l, deg; + sscanf(buf, "%f %f %f %f %f",&a1,&x, &y, &l, °); + Device *d = new PlainRefl(x, y, l, deg); my_device.push_back(d); - printf("New lens f<0 was created\n"); - break; - }*/ - case 4: //ploskoparallell plastinka + printf("New mirror was created\n"); + break; + } + + case 4: //ploskoparallell plastinka == disc { - float a1, x, y, len, wid, n, angle; - sscanf(buf, "%f %f %f %f %f %f %f",&a1,&x, &y, &len, &wid, &angle,&n); - printf("POKAZ: %f", n); - Device *d = new Disc(x, y, len, wid, angle, n); + float a1, x, y, len, wid, deg, n; + sscanf(buf, "%f %f %f %f %f %f %f",&a1,&x, &y, &len, &wid, °, &n); + Device *d = new Disc(x, y, len, wid, deg, n); my_device.push_back(d); printf("New ploskoparallell plastinka was created\n"); break; @@ -136,30 +146,32 @@ int cs=*(int *)arg; printf("New laser was created\n"); break; } + case 6: //triangle prism + { + float a1, x1, y1, x2, y2, x3, y3, n; + sscanf(buf, "%f %f %f %f %f %f %f %f",&a1,&x1, &y1, &x2, &y2, &x3, &y3, &n); + int num = 1; + Device *d = new Prism(num,x1, y1, x2, y2, x3, y3, n); + my_device.push_back(d); + printf("New triangle prism was created\n"); + break; + } + case 7: //sphere mirror { - float a1, x, y, r, deg1, deg2; - sscanf(buf, "%f %f %f %f %f %f",&a1,&x, &y, &r, °1, °2); -// Device *d = new Lens(x, y, l, deg, f); -// my_device.push_back(d); + float a1, x, y, r0, deg_1, deg_2; + sscanf(buf, "%f %f %f %f %f %f",&a1, &x, &y, °_1, °_2, &r0); + Device *d = new SphereRefl(x, y, r0, deg_1-90, deg_2-90); + my_device.push_back(d); printf("New sphere mirror was created\n"); break; } - case 3: //mirror - { - float a1, x, y, deg; - sscanf(buf, "%f %f %f %f",&a1,&x, &y, °); -// Device *d = new Lens(x, y, l, deg, f); -// my_device.push_back(d); - printf("New mirror was created\n"); - break; - } - case 6: //triangle prism + case 8: //wide length { - float a1, x1, y1, x2, y2, x3, y3, n; - sscanf(buf, "%f %f %f %f %f %f %f %f",&a1,&x1, &y1, &x2, &y2, &x3, &y3, &n); -// Device *d = new Lens(x, y, l, deg, f); -// my_device.push_back(d); + float a1, x, y, l, deg, r1, r2, n, de; + sscanf(buf, "%f %f %f %f %f %f %f %f %f",&a1, &x, &y, &r1, &r2, °, &n, &l, &de); + Device *d = new Lens_wide(x, y, l, deg, r1, r2, n, de); + my_device.push_back(d); printf("New triangle prism was created\n"); break; } @@ -178,14 +190,7 @@ int cs=*(int *)arg; break; } } -// Let's imagine we have done it -//first: пробегаем весь отсортированный массив девайсов. Первое пересечение -> break. Пусть пересекло первым второй девайс -//Тогда запускаем новый пробег for 3 to n. И так далее пока не дошли до конца. Если мы успешно прошли весь цикл или сделали брейк на энтом, то -//Запускаем проверку для выходного луча. Пересечет ли он экран? Если да, то в какой точке????. -//После нахождения каждой из точек отправляем Лёне запись. write(wr, "точка1, точка 2", 3). -// Here we need to sort vector my_device by x - sort_(my_device); point *cross = NULL; @@ -209,75 +214,66 @@ int cs=*(int *)arg; //let's work with laser first for(int I=0; I cross_point(my_laser_ray[I]); - if (cross != NULL) - { - sprintf(buf_, "%f %f %f %f %c", my_laser_ray[I]->x, my_laser_ray[I]->y, cross->x, cross->y, '\0');//new dot - printf("THIS IS RAY: %s\n", buf_); - if(send(cs, buf_, strlen(buf_)+1, MSG_NOSIGNAL)==-1) - { - perror("Can't send:"); - return NULL; - } - recv(cs, temp, 1, 0); - k = i + 1; - q = true; - /*my_laser_ray->x = cross->x; - my_laser_ray->y = cross->y;*/ - float tx=cross->x; - float ty=cross->y; - my_device[i]->change_direction(my_laser_ray[I], cross); - if(my_device[i]->getID()==4) - { - sprintf(buf_, "%f %f %f %f %c", tx, ty, my_laser_ray[I]->x, my_laser_ray[I]->y, 0); - printf("THIS IS RAY: %s\n", buf_); - if(send(cs, buf_, strlen(buf_)+1, MSG_NOSIGNAL)==-1) - { - perror("Can't send:"); - return NULL; - } - recv(cs, temp, 1, 0); - } - break; - } + + int num = first(my_device, my_laser_ray[I]); + while(num != -1){ + int num = first(my_device, my_laser_ray[I]); + if (num != -1){ + cross = my_device[num]->cross_point(my_laser_ray[I]); + sprintf(buf_, "%f %f %f %f %c", my_laser_ray[I]->x, my_laser_ray[I]->y, cross->x, cross->y, '\0');//new dot + if(send(cs, buf_, strlen(buf_)+1, MSG_NOSIGNAL)==-1){ + perror("Can't send:"); + return NULL; + } + recv(cs, temp, 1, 0); + float tx=cross->x; + float ty=cross->y; + my_device[num]->change_direction(my_laser_ray[I], cross); + if(my_device[num]->getID()==4){ + sprintf(buf_, "%f %f %f %f %c", tx, ty, my_laser_ray[I]->x, my_laser_ray[I]->y, 0); + if(send(cs, buf_, strlen(buf_)+1, MSG_NOSIGNAL)==-1){ + perror("Can't send:"); + return NULL; + } + recv(cs, temp, 1, 0); + } } - if (q == false){ - printf("%d\n",I); + else{ break; } } -} //cross screen - for(int I=0; Icross_point(my_laser_ray[I]); - if (cross != NULL){ - sprintf(buf_, "%f %f %f %f %c", my_laser_ray[I]->x, my_laser_ray[I]->y, cross->x, cross->y, '\0'); - if(send(cs, buf_, strlen(buf_)+1, MSG_NOSIGNAL)==-1) + float tx, ty, tdeg; + tx=my_laser_ray[I]->x; + ty=my_laser_ray[I]->y; + tdeg=my_laser_ray[I]->deg; + int s_num = first_s(my_screen, my_laser_ray[I]); + if (s_num == -1){ + //find граница, куда дойдет луч + float retx, rety; + retx = my_laser_ray[I]->x + 2000 * cos(GradToRad(tdeg)); + rety = my_laser_ray[I]->y - 2000 * sin(GradToRad(tdeg)); + sprintf(buf_, "%f %f %f %f %c", my_laser_ray[I]->x, my_laser_ray[I]->y, retx, rety, '\0'); + if(send(cs, buf_, strlen(buf_)+1, MSG_NOSIGNAL)==-1) { - perror("Can't send:"); - return NULL; - } - recv(cs, temp, 1, 0); + perror("Can't send:"); + return NULL; + } + recv(cs, temp, 1, 0); + } else{ - //find граница, куда дойдет луч -// sprintf(buf_, "%f %f", ); -// sprintf(buf_, "\0"); - if(send(cs, buf_, strlen(buf_)+1, MSG_NOSIGNAL)==-1) + + cross = my_screen[s_num]->cross_point(my_laser_ray[I]); + sprintf(buf_, "%f %f %f %f %c", tx, ty, crossing->x, crossing->y, '\0'); + if(send(cs, buf_, strlen(buf_)+1, MSG_NOSIGNAL)==-1) { - perror("Can't send:"); - return NULL; - } -recv(cs, temp, 1, 0); + perror("Can't send:"); + return NULL; + } + recv(cs, temp, 1, 0); } - } +} if(send(cs, "FINISH\0", 7, MSG_NOSIGNAL)==-1) { perror("Can't send:"); diff --git a/ray.h b/ray.h index 702af13..fd89441 100644 --- a/ray.h +++ b/ray.h @@ -6,14 +6,39 @@ using namespace std; #define PI 3.14159265 -#define NUMBER 10 // HERE YOU CAN CHANGE NUMBER OF CREATED RAYS {360 : (NUMBBER - 1)} - +#define NUMBER 37 // HERE YOU CAN CHANGE NUMBER OF CREATED RAYS {360 : (NUMBBER - 1)} struct point { float x ; float y ; }; +inline float GradToRad(float deg) { return (float)(deg*PI/180.0);}; +inline float RadToGrad(float arg) { return (float)(arg*180.0/PI);}; +inline float Sqr(float x) {return x*x;} + +int orient_ (float x1, float y1, float x2, float y2, float x3, float y3, float this_deg) {// x1,y1; x2,y2 - line x3,y3 - point + // cout << "x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 << " x3 = " << x1 << " y3 = " << y3 << "\n"; + if (this_deg == 0){ + if (y3 <= y2){ + + // cout << "trying 1 \n"; + return 1; + } + else{ + // cout << "trying -1 \n"; + return -1; + } + } + else { + if (((x3 -x1) * (y2 - y1) - (y3 -y1) * (x2 - x1)) >= 0) + return 1; + else + return -1; + } + +} +/* class RAY{ public: float x, y; @@ -34,9 +59,166 @@ class RAY{ ~RAY (){ printf("Ray was destructed\n"); } +};*/ + +/*class RAY +{ +public: + float x, y; + float deg; + +public: + void set_ray_pos (float x_0, float y_0, float deg_0) + { x = x_0; y = y_0; deg = deg_0;} + void show_ray_pos(int i) + { cout <y - pp->y), (pp->x - this->x))); + r_deg_delta = Deg360(r_deg_delta); + float r_deg = this->deg; + if (r_deg == 360) r_deg = 0; + if (fabs(r_deg - r_deg_delta) < 1) return true; + return false; + } + + void show_ray_pos(int i) + { cout << "Ray(" << i << "):" << " x = " << x << " y = " << y << " deg = " << deg << "\n";} + + float Deg360(float deg1) const + { // угол в диапазоне [0..360] + do { + if (deg1 < 0) deg1 += 360; + if (deg1 >360) deg1 -= 360; + } + while (deg1 < 0 || deg1 > 360); + return deg1; + } + + ~RAY () + { + cout << "Destructure of the ray (" << deg << ") x: " << x << " y: " << y <<"\n"; + } }; class SCREEN +{ + public : + float x1, x2, y1, y2,deg; +public: + SCREEN (float x, float y, float l_0, float deg_0) { + + float x_1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180); + float x_2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180); + float y_1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180); + float y_2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180); + + + deg = deg_0; + + x1 = x_1 * cos (deg * PI / 180) - y_1 * sin (deg * PI / 180); + x2 = x_2 * cos (deg * PI / 180) - y_2 * sin (deg * PI / 180); + y1 = x_1 * sin (deg * PI / 180) + y_1 * cos (deg * PI / 180); + y2 = x_2 * sin (deg * PI / 180) + y_2 * cos (deg * PI / 180); + + } + void screen_pos() + { cout << " x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 <<"\n";} + + point * cross_point (RAY * r) { + point * p = new point (); + float p_x, p_y; + float r_x, r_y; + r_x = r->x; + r_y = r->y; + r->x = r_x * cos (this->deg * PI / 180) - r_y * sin (this->deg * PI / 180); + r->y = r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + if (r->deg >= this->deg) + r->deg = r->deg - this->deg; + else + r->deg = 360 + r->deg -this->deg; + + if ((orient_ (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))){ + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->y >= y1 ) && ( p->y <= y2 )){ + p_x = p->x; + p_y = p->y; + p->x = p_x * cos (this->deg * PI / 180) + p_y * sin (this->deg * PI / 180); + p->y = - p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); + + r_x = r->x; + r_y = r->y; + r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); + r->y = - r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + r->deg = deg + this->deg; + if (r->deg > 360) + r->deg = r->deg - 360; + delete r; + return p; + } + } + if ((orient_ (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + cout << "p->x = " << p->x << "\n" ; + p->y = (float)det_2/det; + cout << "p->x = " << p->x << "\n" ; + if (( p->y >= y1 ) && ( p->y <= y2 )) { + p_x = p->x; + p_y = p->y; + p->x = p_x * cos (this->deg * PI / 180) + p_y * sin (this->deg * PI / 180); + p->y = - p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); + + r_x = r->x; + r_y = r->y; + r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); + r->y = - r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + r->deg = deg + this->deg; + if (r->deg > 360) + r->deg = r->deg - 360; + delete r; + return p; + } + } + + r_x = r->x; + r_y = r->y; + r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); + r->y = - r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); + r->deg = deg + this->deg; + if (r->deg > 360) + r->deg = r->deg - 360; + return NULL; + } + + ~SCREEN () + { + cout << "Destructure of the screen" << "\n"; + } +}; +/*class SCREEN { public : float x1, x2, y1, y2; @@ -74,26 +256,27 @@ public : cout << "Destructure of the screen" << "\n"; } }; - -/*class SOURCE +*/ +class SOURCE { -private: +public: float x, y; - + public: SOURCE (float x_0, float y_0) : x(x_0), y(y_0) { } void source_pos() { cout << " x = " << x << " y = " << y << "\n";} - RAY * rays_create () + RAY** rays_create () { const int num_of_rays = NUMBER; float deg_step = (float)360 / (num_of_rays - 1); - RAY * ray = new RAY [num_of_rays]; + RAY ** ray = new RAY *[num_of_rays]; float deg_i = 0; for (int i = 0; i < num_of_rays; i++) { - ray[i].set_ray_pos (x, y, deg_i); - ray[i].show_ray_pos(i); + ray[i]=new RAY(); + ray[i]->set_ray_pos (x, y, deg_i); + ray[i]->show_ray_pos(i); deg_i = deg_i + deg_step; } return ray; @@ -102,7 +285,10 @@ public : { cout << "Destructure of the source" << "\n"; } -};*/ +}; + + +/* class Laser{ public: RAY *ray; @@ -113,3 +299,144 @@ class Laser{ printf("Laser destructed\n"); } }; +*/ + + + +/*class SCREEN +{ + public : + float x1, x2, y1, y2,deg; +public: + SCREEN (float x, float y, float l_0, float deg_0) { + x1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180); + x2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180); + y1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180); + y2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180); + deg = deg_0; + } + void screen_pos() + { cout << " x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 <<"\n";} + + point * cross_point (RAY * r) { + point * p = new point (); + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))){ + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + cout << "p->x = " << p->x << "\n" ; + p->y = (float)det_2/det; + cout << "p->x = " << p->x << "\n" ; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } return NULL; + } + + ~SCREEN () + { + cout << "Destructure of the screen" << "\n"; + } +}; + +class LASER +{ +private: + float x, y, deg; + +public: + LASER (float x_0, float y_0, float deg_0) : + x(x_0), y(y_0), deg (deg_0) + { } + void laser_pos() + { cout << " x = " << x << " y = " << y << "\n";} + RAY * rays_create () + { + RAY * ray = new RAY (); + ray->set_ray_pos (x, y, deg); + ray->show_ray_pos(0); + return ray; + } + ~LASER () + { + cout << "Destructure of the source" << "\n"; + } +}; +class SCREEN +{ + public : + float x1, x2, y1, y2,deg; +public: + SCREEN (float x, float y, float l_0, float deg_0) { + x1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180); + x2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180); + y1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180); + y2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180); + deg = deg_0; + } + void screen_pos() + { cout << " x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 <<"\n";} + + point * cross_point (RAY * r) { + point * p = new point (); + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))){ + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + p->y = (float)det_2/det; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } + if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { + float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); + float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); + float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); + p->x = (float)det_1/det; + cout << "p->x = " << p->x << "\n" ; + p->y = (float)det_2/det; + cout << "p->x = " << p->x << "\n" ; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) + return p; + } return NULL; + } + + ~SCREEN () + { + cout << "Destructure of the screen" << "\n"; + } +}; +*/ +class LASER +{ +private: + float x, y, deg; + +public: + LASER (float x_0, float y_0, float deg_0) : + x(x_0), y(y_0), deg (deg_0) + { } + void laser_pos() + { cout << " x = " << x << " y = " << y << "\n";} + RAY * rays_create () + { + RAY * ray = new RAY (); + ray->set_ray_pos (x, y, deg); + ray->show_ray_pos(0); + return ray; + } + ~LASER () + { + cout << "Destructure of the source" << "\n"; + } +}; + From 8cbebc9f30c3761ced7428b2a881bae49d543b1d Mon Sep 17 00:00:00 2001 From: AntonGerasimov Date: Sun, 17 May 2015 21:34:21 +0300 Subject: [PATCH 12/19] ver 051715 --- Makefile | 6 ---- device.h => main/device.h | 59 +++++++----------------------- first.h => main/first.h | 0 main.cc => main/main.cc | 19 ++++------ ray.h => main/ray.h | 76 ++++++--------------------------------- manual | 35 ------------------ 6 files changed, 31 insertions(+), 164 deletions(-) delete mode 100644 Makefile rename device.h => main/device.h (97%) rename first.h => main/first.h (100%) rename main.cc => main/main.cc (97%) rename ray.h => main/ray.h (82%) delete mode 100644 manual diff --git a/Makefile b/Makefile deleted file mode 100644 index 7a4b604..0000000 --- a/Makefile +++ /dev/null @@ -1,6 +0,0 @@ -all: - g++ main.cc -o a -Wall -lpthread -clean: - rm -r -f a -debug: - g++ main.cc -o a -Wall -lpthread -g diff --git a/device.h b/main/device.h similarity index 97% rename from device.h rename to main/device.h index f886042..727f243 100644 --- a/device.h +++ b/main/device.h @@ -5,6 +5,7 @@ using namespace std; #define PI 3.14159265 +#define NUMBER 10 // HERE YOU CAN CHANGE NUMBER OF CREATED RAYS {360 : (NUMBBER - 1)} float LenVect(point *p1, point *p2) { @@ -62,7 +63,6 @@ class Device{ } virtual point * cross_point (RAY * r) const = 0; }; - class Disc : public Device{ // n > 1 !!! public: float w,l,n, x3,y3, x4, y4, deg; // w - width @@ -148,15 +148,10 @@ class Disc : public Device{ // n > 1 !!! p_y = p->y; p->x = p_x * cos (this->deg * PI / 180) + p_y * sin (this->deg * PI / 180); p->y = - p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); - r_x = r->x; r_y = r->y; r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); - r->y = - r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); - r->deg = deg + this->deg; - if (r->deg > 360) - r->deg = r->deg - 360; - + r->y = -r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); return p; } } @@ -172,15 +167,10 @@ class Disc : public Device{ // n > 1 !!! p_y = p->y; p->x = p_x * cos (this->deg * PI / 180) + p_y * sin (this->deg * PI / 180); p->y = - p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); - r_x = r->x; r_y = r->y; r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); - r->y = - r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); - r->deg = deg + this->deg; - if (r->deg > 360) - r->deg = r->deg - 360; - + r->y = -r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); return p; } } @@ -206,15 +196,10 @@ class Disc : public Device{ // n > 1 !!! p_y = p->y; p->x = p_x * cos (this->deg * PI / 180) + p_y * sin (this->deg * PI / 180); p->y = - p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); - r_x = r->x; r_y = r->y; r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); - r->y = - r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); - r->deg = deg + this->deg; - if (r->deg > 360) - r->deg = r->deg - 360; - + r->y = -r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); return p; } } @@ -235,47 +220,30 @@ class Disc : public Device{ // n > 1 !!! p_y = p->y; p->x = p_x * cos (this->deg * PI / 180) + p_y * sin (this->deg * PI / 180); p->y = - p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); - r_x = r->x; r_y = r->y; r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); - r->y = - r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); - r->deg = deg + this->deg; - if (r->deg > 360) - r->deg = r->deg - 360; - + r->y = -r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); return p; } } - r_x = r->x; - r_y = r->y; - r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); - r->y = - r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); - r->deg = deg + this->deg; - if (r->deg > 360) - r->deg = r->deg - 360; + return NULL; } void change_direction(RAY * r, point * p ) const { - //cout<< "this->n = " << this->n << "\n"; - float p_x, p_y, r_x, r_y; + float p_x, p_y, r_x,r_y; p_x = p->x; p_y = p->y; p->x = p_x * cos (this->deg * PI / 180) - p_y * sin (this->deg * PI / 180); p->y = p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); - r_x = r->x; r_y = r->y; r->x = r_x * cos (this->deg * PI / 180) - r_y * sin (this->deg * PI / 180); r->y = r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); - if (r->deg >= this->deg) - r->deg = r->deg - this->deg; - else - r->deg = 360 + r->deg -this->deg; point * new_p = p; float deg = 0; @@ -373,7 +341,6 @@ class Disc : public Device{ // n > 1 !!! new_p->x = p->x - this->l * tan ( beta * PI / 180); } else { - //cout << beta ; beta = fabs (beta); new_p->y = p->y - this->l; new_p->x = p->x + this->l * tan ( beta * PI / 180); @@ -424,7 +391,7 @@ class Disc : public Device{ // n > 1 !!! } } } - // cout << "beta = " << beta << "\n"; + //cout << "beta = " << beta << "\n"; r->x = new_p->x * cos (this->deg * PI / 180) + new_p->y * sin (this->deg * PI / 180) ; r->y = new_p->y * cos (this->deg * PI / 180) - new_p->x * sin (this->deg * PI / 180); r->deg = deg + this->deg; @@ -1147,7 +1114,7 @@ class SphereRefl : public Device { float deg_t; if (fabs(delta) < 0.1) { deg_t = RadToGrad(atan((OptC->x - p->x) / (OptC->y - p->y))); - // if (OptC->x - p->x<0)deg_t +=180; + if (OptC->x - p->x < 0) deg_t += 180; deg_t = r->Deg360(deg_t); std::cout << "Sphere Point 1: x=" << p->x << " y=" << p->y << " Sphere Deg=" << deg_t << " deg1=" << deg1 << " deg2=" << deg2 << "\n"; @@ -1237,7 +1204,7 @@ class Gran { point *p = new point(); float p_check = (r->x - p1->x) * (p2->y - p1->y) - (r->y - p1->y) * (p2->x - p1->x); // источник луча находится на грани призмы if (fabs(p_check) < 0.1) return NULL; - float x1, y1, x2, y2, r1, r2; + float x1, y1, x2, y2, r1, r2, k_ray, b_ray; float det, det1, det2; float xp, yp; @@ -1308,7 +1275,7 @@ class Prism : public Device { gran_vect.push_back(gran_t); std::cout << "\n Create Prism N: " << N << "\n"; - for (unsigned i = 0; i < P.size();i++) { + for (int i = 0; i < P.size();i++) { std::cout << "Point " << i << " x: " << P[i]->x << " y: " << P[i]->y << "\n"; } std::cout << "Coeff_N: " << n_coeff << "\n"; @@ -1318,7 +1285,7 @@ class Prism : public Device { point *p = NULL; float len_min = 1e10; - for (unsigned i = 0; i < gran_vect.size(); i++) { + for (int i = 0; i < gran_vect.size(); i++) { point *p_t = gran_vect[i]->cross_point(r); // проверка грани призмы и луча if (p_t != NULL) { if (r->CheckRayPoint(p_t)) { @@ -1347,7 +1314,7 @@ class Prism : public Device { Gran *FindGran(point *pp) const { float cos_a = 0; - for (unsigned i=0; i < gran_vect.size(); i++) { // нашли грань для точки p + for (int i=0; i < gran_vect.size(); i++) { // нашли грань для точки p cos_a = gran_vect[i]->CheckPointCosA(pp); if (fabs(cos_a + 1) < 0.1) { diff --git a/first.h b/main/first.h similarity index 100% rename from first.h rename to main/first.h diff --git a/main.cc b/main/main.cc similarity index 97% rename from main.cc rename to main/main.cc index cdf82cc..a3290a0 100644 --- a/main.cc +++ b/main/main.cc @@ -62,10 +62,10 @@ int main() void *func(void* arg) { - int rd; - char buf[512]; - int cs=*(int *)arg; - char buf_[512]; +int rd; +char buf[512]; +int cs=*(int *)arg; + char buf_[32]; vector my_device; vector my_screen; @@ -243,16 +243,12 @@ for(int I=0; Ix; - ty=my_laser_ray[I]->y; - tdeg=my_laser_ray[I]->deg; int s_num = first_s(my_screen, my_laser_ray[I]); if (s_num == -1){ //find граница, куда дойдет луч float retx, rety; - retx = my_laser_ray[I]->x + 2000 * cos(GradToRad(tdeg)); - rety = my_laser_ray[I]->y - 2000 * sin(GradToRad(tdeg)); + retx = my_laser_ray[I]->x + 2000 * cos(GradToRad(my_laser_ray[I]->deg)); + rety = my_laser_ray[I]->y - 2000 * sin(GradToRad(my_laser_ray[I]->deg)); sprintf(buf_, "%f %f %f %f %c", my_laser_ray[I]->x, my_laser_ray[I]->y, retx, rety, '\0'); if(send(cs, buf_, strlen(buf_)+1, MSG_NOSIGNAL)==-1) { @@ -263,9 +259,8 @@ for(int I=0; Icross_point(my_laser_ray[I]); - sprintf(buf_, "%f %f %f %f %c", tx, ty, crossing->x, crossing->y, '\0'); + sprintf(buf_, "%f %f %f %f %c", my_laser_ray[I]->x, my_laser_ray[I]->y, cross->x, cross->y, '\0'); if(send(cs, buf_, strlen(buf_)+1, MSG_NOSIGNAL)==-1) { perror("Can't send:"); diff --git a/ray.h b/main/ray.h similarity index 82% rename from ray.h rename to main/ray.h index fd89441..1840228 100644 --- a/ray.h +++ b/main/ray.h @@ -6,7 +6,8 @@ using namespace std; #define PI 3.14159265 -#define NUMBER 37 // HERE YOU CAN CHANGE NUMBER OF CREATED RAYS {360 : (NUMBBER - 1)} +#define NUMBER 10 // HERE YOU CAN CHANGE NUMBER OF CREATED RAYS {360 : (NUMBBER - 1)} + struct point { float x ; @@ -117,65 +118,32 @@ class RAY } }; + class SCREEN { public : float x1, x2, y1, y2,deg; public: SCREEN (float x, float y, float l_0, float deg_0) { - - float x_1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180); - float x_2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180); - float y_1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180); - float y_2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180); - - + x1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180); + x2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180); + y1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180); + y2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180); deg = deg_0; - - x1 = x_1 * cos (deg * PI / 180) - y_1 * sin (deg * PI / 180); - x2 = x_2 * cos (deg * PI / 180) - y_2 * sin (deg * PI / 180); - y1 = x_1 * sin (deg * PI / 180) + y_1 * cos (deg * PI / 180); - y2 = x_2 * sin (deg * PI / 180) + y_2 * cos (deg * PI / 180); - } void screen_pos() { cout << " x1 = " << x1 << " y1 = " << y1 << " x2 = " << x2 << " y2 = " << y2 <<"\n";} point * cross_point (RAY * r) { point * p = new point (); - float p_x, p_y; - float r_x, r_y; - r_x = r->x; - r_y = r->y; - r->x = r_x * cos (this->deg * PI / 180) - r_y * sin (this->deg * PI / 180); - r->y = r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); - if (r->deg >= this->deg) - r->deg = r->deg - this->deg; - else - r->deg = 360 + r->deg -this->deg; - if ((orient_ (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))){ float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); p->x = (float)det_1/det; p->y = (float)det_2/det; - if (( p->y >= y1 ) && ( p->y <= y2 )){ - p_x = p->x; - p_y = p->y; - p->x = p_x * cos (this->deg * PI / 180) + p_y * sin (this->deg * PI / 180); - p->y = - p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); - - r_x = r->x; - r_y = r->y; - r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); - r->y = - r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); - r->deg = deg + this->deg; - if (r->deg > 360) - r->deg = r->deg - 360; - delete r; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) return p; - } } if ((orient_ (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); @@ -185,32 +153,9 @@ class SCREEN cout << "p->x = " << p->x << "\n" ; p->y = (float)det_2/det; cout << "p->x = " << p->x << "\n" ; - if (( p->y >= y1 ) && ( p->y <= y2 )) { - p_x = p->x; - p_y = p->y; - p->x = p_x * cos (this->deg * PI / 180) + p_y * sin (this->deg * PI / 180); - p->y = - p_x * sin (this->deg * PI / 180) + p_y * cos (this->deg * PI / 180); - - r_x = r->x; - r_y = r->y; - r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); - r->y = - r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); - r->deg = deg + this->deg; - if (r->deg > 360) - r->deg = r->deg - 360; - delete r; + if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) return p; - } - } - - r_x = r->x; - r_y = r->y; - r->x = r_x * cos (this->deg * PI / 180) + r_y * sin (this->deg * PI / 180); - r->y = - r_x * sin (this->deg * PI / 180) + r_y * cos (this->deg * PI / 180); - r->deg = deg + this->deg; - if (r->deg > 360) - r->deg = r->deg - 360; - return NULL; + } return NULL; } ~SCREEN () @@ -218,6 +163,7 @@ class SCREEN cout << "Destructure of the screen" << "\n"; } }; + /*class SCREEN { public : diff --git a/manual b/manual deleted file mode 100644 index ba7a020..0000000 --- a/manual +++ /dev/null @@ -1,35 +0,0 @@ -source: 0 10 20 N (360/(N-1))=m, m - целое число. Например, N = 19, N = 37. - source x y количество лучей, которое создается - -screen: 1 x y length deg - screen - -lens : 2 23 25 10 0 10 -f>0 lens x y length deg_0 focus - -lens: 3 f>0 -f<0 lens x y length deg_0 focus - -plosk: 4 x y length width n -plast -плоскопараллельная пластинка - -laser: 5 x y deg -лазер mirror - -sphere mirror: 6 x y r deg1 deg2 - радиус угол, под которым ... нижнюю часть - кривизны мы видим верхнюю - часть зеркала - -mirror: 7 x y deg -зеркало - -prism: 8 x1 y1 x2 y2 x3 y3 n -треугольная точки треугольника -призма - -Все значения - float -Все углы - в градусах -x,y - координаты центра -// From 0f4ec2ea583d391158825ec655561ef407243b3a Mon Sep 17 00:00:00 2001 From: AntonGerasimov Date: Mon, 18 May 2015 16:34:09 +0300 Subject: [PATCH 13/19] Add () to size --- main/first.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/first.h b/main/first.h index b7e54ff..47c839a 100644 --- a/main/first.h +++ b/main/first.h @@ -24,7 +24,7 @@ int first(vector d, RAY *r){ float y = r->y; float rast; float min = 100000; - if (d.size==0){ + if (d.size()==0){ return ret; } for (int i = 0; i < d.size(); i++){ From 70f035ed61a793cb1c35f2987dd03c60b11b9304 Mon Sep 17 00:00:00 2001 From: AntonGerasimov Date: Mon, 18 May 2015 16:46:20 +0300 Subject: [PATCH 14/19] change unsigned to int in remoteLen --- main/main.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/main.cc b/main/main.cc index a3290a0..16e45c9 100644 --- a/main/main.cc +++ b/main/main.cc @@ -46,7 +46,7 @@ int main() while(1) { struct sockaddr_in remote; - unsigned remoteLen=sizeof(remote); + int remoteLen=sizeof(remote); int cs; if((cs=accept(h, (sockaddr *)&remote, &remoteLen))<0){ perror("Accepting"); From df0a6d1e4610adc826cc64ec80776773c76c0e52 Mon Sep 17 00:00:00 2001 From: AntonGerasimov Date: Mon, 18 May 2015 16:49:23 +0300 Subject: [PATCH 15/19] Add Makefile --- main/Makefile | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 main/Makefile diff --git a/main/Makefile b/main/Makefile new file mode 100644 index 0000000..47ef3ec --- /dev/null +++ b/main/Makefile @@ -0,0 +1,8 @@ +all: clean + g++ main.cc -o a -Wall -lpthread +clean: + rm -r -f a OpticLabServer +debug: clean + g++ main.cc -o a -Wall -lpthread -g +release: clean + g++ main.cc -o OpticLabServer -Wall -lpthread -O3 From 1ffaf1826d3b934a3f50b22b2e2035b7c26b4662 Mon Sep 17 00:00:00 2001 From: AntonGerasimov Date: Mon, 18 May 2015 17:23:57 +0300 Subject: [PATCH 16/19] Add files --- main/main.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/main.cc b/main/main.cc index 16e45c9..a3290a0 100644 --- a/main/main.cc +++ b/main/main.cc @@ -46,7 +46,7 @@ int main() while(1) { struct sockaddr_in remote; - int remoteLen=sizeof(remote); + unsigned remoteLen=sizeof(remote); int cs; if((cs=accept(h, (sockaddr *)&remote, &remoteLen))<0){ perror("Accepting"); From 42733d2f69a126d7ba9b0f5ab858d98aa3b198b3 Mon Sep 17 00:00:00 2001 From: AntonGerasimov Date: Mon, 18 May 2015 17:29:48 +0300 Subject: [PATCH 17/19] Prism change direction --- main/device.h | 231 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 169 insertions(+), 62 deletions(-) diff --git a/main/device.h b/main/device.h index 727f243..aa8527a 100644 --- a/main/device.h +++ b/main/device.h @@ -1325,76 +1325,183 @@ class Prism : public Device { return NULL; } - void change_direction(RAY *r, point *p) const // Призма + void change_direction(RAY *r, point *p) const // Ïðèçìà + { - std::cout << "\nPrev r->deg: " << r->deg << "\n"; // исходный угол луча - std::cout << "Point_in: x: " << p->x << " y: " << p->y << " \n"; - std::cout << "Coeff n : " << n_coeff << " \n"; - float cos_a = 0; - Gran *gran = FindGran(p); - - point *r0 = new point(); - r0->x = r->x; - r0->y = r->y; - - cos_a = Scalar(SubVect(p, r0), gran->normal) / (LenVect(p, r0) * sqrt(Sqr(gran->normal->x) + Sqr(gran->normal->y))); - float deg_f = RadToGrad(acos(cos_a)); - std::cout << "Deg_in: " << deg_f << "\n"; - while (fabs(deg_f) > 90 ) { - deg_f = 180 - deg_f; - std::cout << "Deg_in: " << deg_f << " 180-a \n"; - } - float deg_r = RadToGrad(asin(sin(GradToRad(deg_f)) / n_coeff)); - std::cout << "Deg_r_in: " << deg_r << "\n"; - while (fabs(deg_r) > 90 ) { - deg_r = 180 - deg_r; - std::cout << "Deg_r_in: " << deg_r << " 180-a \n"; - } - float deg_r_new = r->deg - deg_f + deg_r; + point *r0 = new point(); - std::cout << "Deg_ray_new " << deg_r_new << "\n"; + Gran *gran_in = NULL; - RAY *ray_in = new RAY; - ray_in->set_ray_pos (p->x, p->y, ray_in->Deg360(deg_r_new)); + r0->x = r->x; - point *p_new = cross_point(ray_in); // выходная точка - if (p_new != NULL) - { - Gran *gran_out = FindGran(p_new); - std::cout << "\n Point_out: x: " << p_new->x << " y: " << p_new->y << "\n"; - - cos_a = Scalar(SubVect(p_new, r0), gran_out->normal) / (LenVect(p_new, r0) * sqrt(Sqr(gran_out->normal->x) + Sqr(gran_out->normal->y))); - float deg_r_out = RadToGrad(acos(cos_a)); // угол между лучом и выходной нормалью - std::cout << " Deg_r_out: " << deg_r_out << "\n"; - while (fabs(deg_r_out) > 90 ) { - deg_r_out = 180 - deg_r_out; // 0..90 - std::cout << " Deg_r_out: " << deg_r_out << " 180-a \n"; - } - if (sin(GradToRad(deg_r_out)) * n_coeff > 1) { - std::cout << " sin(Deg_i_out) * n > 1: " << sin(GradToRad(deg_r_out)) * n_coeff << "\n"; - deg_r_out = 90 - deg_r_out; - } - float deg_i_out = RadToGrad(asin(sin(GradToRad(deg_r_out)) * n_coeff)); // sin(i_out) = sin(r_out) * n - std::cout << " Deg_i_out: " << deg_i_out << "\n"; - while (fabs(deg_i_out) > 90 ) { - deg_i_out = 180 - deg_i_out; // 0..90 - std::cout << " Deg_i_out: " << deg_i_out << " 180-a \n"; - } + r0->y = r->y; + + std::cout << "\nPrev r->deg: " << r->deg << "\n"; // èñõîäíûé óãîë ëó÷à + + std::cout << "Point_in: x: " << p->x << " y: " << p->y << " \n"; + + std::cout << "Coeff n : " << n_coeff << " \n"; + + float cos_a = 0; + + Gran *gran = FindGran(p); + + if (gran == NULL) + + { + + cout << "Point P out of prizm. x: " << p->x << " y:" << p->y << " \n"; + + return; + + } + + gran_in = FindGran(r0); + + if (gran_in == NULL) { // Ïåðâûé ïðîõîä (ëó÷ èçâíå) + + cout << "Prism first begin." << "\n"; + + cos_a = Scalar(SubVect(p, r0), gran->normal) / (LenVect(p, r0) * sqrt(Sqr(gran->normal->x) + Sqr(gran->normal->y))); + + float deg_f = RadToGrad(acos(cos_a)); + + std::cout << "Deg_in: " << deg_f << "\n"; + + while (fabs(deg_f) > 90 ) { + + deg_f = 180 - deg_f; + + std::cout << "Deg_in: " << deg_f << " 180-a \n"; + + } + + float deg_r = RadToGrad(asin(sin(GradToRad(deg_f)) / n_coeff)); + + std::cout << "Deg_r_in: " << deg_r << "\n"; + + while (fabs(deg_r) > 90 ) { + + deg_r = 180 - deg_r; + + std::cout << "Deg_r_in: " << deg_r << " 180-a \n"; + + } + + + + float deg_r_new = r->deg - deg_f + deg_r; + + + + std::cout << "Deg_ray_new " << deg_r_new << "\n"; + + r->deg = r->Deg360(deg_r_new); + + r->x = p->x; + + r->y = p->y; + + std::cout << "Prism first: New r->deg: " << r->deg << " x: " << r->x << " y: "<< r->y <<"\n"; + + + + return; - float deg_i_out_new = ray_in->deg - deg_i_out + deg_r_out; - std::cout << "Deg_i_out_new: " << deg_i_out_new << "\n"; - r->deg = r->Deg360(deg_i_out_new); - r->x = p_new->x; - r->y = p_new->y; - } - r->x = p->x; - r->y = p->y; - r->deg = r->Deg360(r->deg); // угол луча в [0..360] - std::cout << "New r->deg: " << r->deg << "\n"; - } + } + + else { // âòîðîé ïðîõîä ëó÷à + + cout << "Prism second begin." << "\n"; + + + + //RAY *ray_in = new RAY; + + //ray_in->set_ray_pos (p->x, p->y, ray_in->Deg360(deg_r_new)); + + + + // point *p_new = cross_point(ray_in); // âûõîäíàÿ òî÷êà + + // äîëæíà áûòü âûçâàíà ïîñëå ïåðâîãî âûçîâà change_direction + + point *p_new = new point(); + + p_new->x = p->x; + + p_new->y = p->y; + + RAY *ray_in = r; + + if (p_new != NULL) // òî÷êà âûõîäà óæå íàéäåíà âî âíåøíåì âûçîâå cross_point + + { + + Gran *gran_out = FindGran(p_new); + + std::cout << "\n Point_out: x: " << p_new->x << " y: " << p_new->y << "\n"; + + + + cos_a = Scalar(SubVect(p_new, r0), gran_out->normal) / (LenVect(p_new, r0) * sqrt(Sqr(gran_out->normal->x) + Sqr(gran_out->normal->y))); + + float deg_r_out = RadToGrad(acos(cos_a)); // óãîë ìåæäó ëó÷îì è âûõîäíîé íîðìàëüþ + + std::cout << " Deg_r_out: " << deg_r_out << "\n"; + + while (fabs(deg_r_out) > 90 ) { + + deg_r_out = 180 - deg_r_out; // 0..90 + + std::cout << " Deg_r_out: " << deg_r_out << " 180-a \n"; + + } + + if (sin(GradToRad(deg_r_out)) * n_coeff > 1) { + + std::cout << " sin(Deg_i_out) * n > 1: " << sin(GradToRad(deg_r_out)) * n_coeff << "\n"; + + deg_r_out = 90 - deg_r_out; + + } + + float deg_i_out = RadToGrad(asin(sin(GradToRad(deg_r_out)) * n_coeff)); // sin(i_out) = sin(r_out) * n + + std::cout << " Deg_i_out: " << deg_i_out << "\n"; + + while (fabs(deg_i_out) > 90 ) { + + deg_i_out = 180 - deg_i_out; // 0..90 + + std::cout << " Deg_i_out: " << deg_i_out << " 180-a \n"; + + } + + + + float deg_i_out_new = ray_in->deg - deg_i_out + deg_r_out; + + std::cout << "Deg_i_out_new: " << deg_i_out_new << "\n"; + + + + r->deg = r->Deg360(deg_i_out_new); + + r->x = p_new->x; + + r->y = p_new->y; + + std::cout << "Prism second: New r->deg: " << r->deg << " x: " << r->x << " y: " <y << "\n"; + + } + + } + + } const char *getID() const { return "Prism"; From f6d6530c3f9f4d14cb0a4d02a9fdcd197f153217 Mon Sep 17 00:00:00 2001 From: AntonGerasimov Date: Mon, 18 May 2015 17:30:38 +0300 Subject: [PATCH 18/19] Sphere cross point --- main/device.h | 209 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 152 insertions(+), 57 deletions(-) diff --git a/main/device.h b/main/device.h index aa8527a..15ae8af 100644 --- a/main/device.h +++ b/main/device.h @@ -1074,73 +1074,168 @@ class SphereRefl : public Device { std::cout << "X1:" << x1 << " Y1: " << y1 << " X2: " << x2 << " Y2: " << y2 << "\n"; } - point *cross_point(RAY *r) const { // точка пересечения сферы и луча + + point *cross_point(RAY *r) const { // òî÷êà ïåðåñå÷åíèÿ ñôåðû è ëó÷à + point *p = new point(); - float xn, yn, k_ray, b_ray; - float xc, yc; - if (r->deg != 0) { - xn = r->y / tan (GradToRad(r->deg)) + r->x; - yn = r->x * tan (GradToRad(r->deg)) + r->y; - - k_ray = -yn/xn; // уравнение луча - b_ray = yn; - // пересечение луча и нормали, проходящей через центр сферы - xc = (OptC->y - OptC->x/k_ray - yn) / (k_ray + 1/k_ray); // k_ray = 0 ? - yc = -(xc + OptC->x)/ k_ray + OptC->y; - } else { - xc = OptC->x; - yc = r->y; - } - point pc; - pc.x = xc; - pc.y = yc; - point p_ray; - p_ray.x = r->x; - p_ray.y = r->y; - float l_ray_optc = LenVect(&p_ray, OptC); - float l_ray_pc = LenVect(&p_ray, &pc); - float l_d = sqrt(l_ray_optc * l_ray_optc - l_ray_pc * l_ray_pc); - float l_ray; - if (this->R >= l_d) { - l_ray = fabs(l_ray_pc - sqrt(R * R - l_d * l_d)); - p->x = r->x + l_ray * cos(GradToRad(r->deg)); - p->y = r->y - l_ray * sin(GradToRad(r->deg)); - - float delta = Sqr(p->x - OptC->x) + Sqr(p->y - OptC->y) - Sqr(R); - std::cout << "1) p->x: " << p->x << " p->y: " << p->y << " r->deg: " << r->deg << " delta:" << delta << "\n"; - // Проверка принадлежности сегменту - // Находим угол от вертикали - float deg_t; - if (fabs(delta) < 0.1) { - deg_t = RadToGrad(atan((OptC->x - p->x) / (OptC->y - p->y))); - if (OptC->x - p->x < 0) deg_t += 180; - deg_t = r->Deg360(deg_t); - - std::cout << "Sphere Point 1: x=" << p->x << " y=" << p->y << " Sphere Deg=" << deg_t << " deg1=" << deg1 << " deg2=" << deg2 << "\n"; - if (deg1 <= deg_t && deg_t <= deg2 ) return p; - } + point *p1 = new point(); + + point *p2 = new point(); + + float aa, bb, cc, dd; + + + + float xn, yn, k_ray, b_ray; + + float xc, yc; + + + + if (r->deg == 90 || r->deg == 270) + + { + + dd = Sqr(R) - Sqr(r->x - OptC->x); + + if (dd < 0) return NULL; + + p1->y = OptC->y + sqrt(dd); + + p2->y = OptC->y - sqrt(dd); + + p1->x = r->x; + + p2->x = r->x; + + } else + + + + if (r->deg == 0 || r->deg == 360 || r->deg == 180) + + { + + dd = Sqr(R) - Sqr(r->y - OptC->y); + + if (dd < 0) return NULL; + + p1->x = OptC->x + sqrt(dd); + + p2->x = OptC->x - sqrt(dd); + + p1->y = r->y; + + p2->y = r->y; + + } else + + + + { + + xn = r->y / tan (GradToRad(r->deg)) + r->x; + + yn = r->x * tan (GradToRad(r->deg)) + r->y; + + + + + + aa = 1.0 + Sqr(xn/yn); + + bb = 2.0 * (xn * OptC->x / yn - OptC->y - Sqr(xn)/ yn); + + cc = Sqr(xn - OptC->x) + Sqr(OptC->y) - Sqr(this->R); + + + + dd = Sqr(bb) - 4 * aa * cc; + + if (dd < 0) return NULL; // êîðíåé íåò + + dd = sqrt(dd); + + + + p1->y = (-bb + dd) / (2 * aa); // êîðíè ïåðåñå÷åíèé + + p2->y = (-bb - dd) / (2 * aa); + + p1->x = (1.0 - p1->y/yn) * xn; + + p2->x = (1.0 - p2->y/yn) * xn; + + } + + float delta1 = Sqr(p1->x - OptC->x) + Sqr(p1->y - OptC->y) - Sqr(R); + + float delta2 = Sqr(p2->x - OptC->x) + Sqr(p2->y - OptC->y) - Sqr(R); + + std::cout << "1) p1->x: " << p1->x << " p1->y: " << p1->y << " r->deg: " << r->deg << " delta1:" << delta1 << "\n"; + + std::cout << "2) p2->x: " << p2->x << " p2->y: " << p2->y << " r->deg: " << r->deg << " delta2:" << delta2 << "\n"; + + float p1_deg = RadToGrad(acos((OptC->y - p1->y) / this->R)); + + if (p1->x - OptC->x > 0) p1_deg = 360 - p1_deg; + + float p2_deg = RadToGrad(acos((OptC->y - p2->y) / this->R)); + + if (p2->x - OptC->x > 0) p2_deg = 360 - p2_deg; + + //if (fabs(delta1) < 0.1) { + + + + + + if (ceil(p1_deg) >= deg1 && floor(p1_deg) <= deg2 /*&& delta1 < 0.1*/) { // ëåæèò â ñåãìåíòå ñôåðû + + if (r->CheckRayPoint(p1)) { // Ïðîâåðêà ëó÷à (òî÷êà íàõîäèòñÿ íà ëó÷å) + + p->x = p1->x; + + p->y = p1->y; + + std::cout << "Sphere intersection 1\n"; + + std::cout << "p1->x: " << p1->x << " p1->y: " << p1->y << " r->deg: " << r->deg << "\n"; + + return p; + + } + + } + + if (ceil(p2_deg) >= deg1 && floor(p2_deg) <= deg2 /*&& delta2 < 0.1*/) { // ëåæèò â ñåãìåíòå ýëëèïñà + + if (r->CheckRayPoint(p2)) { // Ïðîâåðêà ëó÷à (òî÷êà íàõîäèòñÿ íà ëó÷å) + + p->x = p2->x; + + p->y = p2->y; + + std::cout << "Sphere intersection 2\n"; + + std::cout << "p2->x: " << p2->x << " p2->y: " << p2->y << " r->deg: " << r->deg << "\n"; + + return p; + + } + + } - // Второй корень - l_ray = fabs(l_ray_pc + sqrt(R * R - l_d * l_d)); - p->x = r->x + l_ray * cos(GradToRad(r->deg)); - p->y = r->y - l_ray * sin(GradToRad(r->deg)); - delta = Sqr(p->x - OptC->x) + Sqr(p->y - OptC->y) - Sqr(R); - std::cout << "2) p->x: " << p->x << " p->y: " << p->y << " r->deg: " << r->deg << " delta:" << delta << "\n"; - if (fabs(delta) < 0.1) { - deg_t = RadToGrad(atan((OptC->x - p->x) / (OptC->y - p->y))); - deg_t = r->Deg360(deg_t); - std::cout << "Sphere Point 2: x=" << p->x << " y=" << p->y << " Sphere Deg=" << deg_t << " deg1=" << deg1 << " deg2=" << deg2 << "\n"; - if (deg1 <= deg_t && deg_t <= deg2 ) return p; - } - } return NULL; + } + void change_direction(RAY *r, point *p) const // Сферическое зеркало { std::cout << "\nPrev r->deg: " << r->deg << "\n"; From d087581797853bffe41e1ea9b2b0ac19bba368df Mon Sep 17 00:00:00 2001 From: AntonGerasimov Date: Mon, 18 May 2015 18:11:58 +0300 Subject: [PATCH 19/19] Add prism id --- main/device.h | 152 ++------------------------------------------------ main/main.cc | 12 ++++ 2 files changed, 18 insertions(+), 146 deletions(-) diff --git a/main/device.h b/main/device.h index 15ae8af..efead99 100644 --- a/main/device.h +++ b/main/device.h @@ -411,147 +411,6 @@ class Disc : public Device{ // n > 1 !!! - -/* -class Lens_ras : public Device{ -public: - float deg, f, l; - -public: - Lens_ras ( float x, float y, float l_0, float deg_0, float f_0){ //deg from vertical 0 <= deg < 90 !!! against hour ; - x1 = x - (float)(l_0/2) * sin (deg_0 * PI / 180) ; - x2 = x + (float)(l_0/2) * sin (deg_0 * PI / 180); - y1 = y - (float)(l_0/2) * cos (deg_0 * PI / 180); - y2 = y + (float)(l_0/2) * cos (deg_0 * PI / 180); - l = l_0; - f = f_0; - deg = deg_0; - } - point * cross_point (RAY * r) const { - point * p = new point (); - if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) && ((r->deg <= 90) || (r->deg >= 270))){ - cout << "c_here_1 \n"; - float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); - float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); - float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); - p->x = (float)det_1/det; - p->y = (float)det_2/det; - if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) - return p; - } - if ((orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) > 0) && ((r->deg >= 90) && (r->deg <= 270))) { - cout << "c_here_2 \n"; - float det = this->y2 - this->y1 - tan (r->deg * PI / 180) * (this->x1 - this->x2); - float det_1 = this->y2 * this->x1 - this->y1 * this->x2 - (this->x1 - this->x2) * (r->y + tan (r->deg * PI / 180) * r->x); - float det_2 = (this->y2 - this->y1) * (r->y + tan (r->deg * PI / 180) * r->x) - (this->y2 * this->x1 - this->y1 * this->x2) * tan (r->deg * PI / 180); - p->x = (float)det_1/det; - p->y = (float)det_2/det; - if (( p->x >= x1 ) && ( p->x <= x2 ) && ( p->y >= y1 ) && ( p->y <= y2 )) - return p; - } - return NULL; - } - - void change_direction(RAY * r, point * p ) const - { - float l_1 = 0; - float alpha = 0; - float alpha_r = 0; - float c = 0 ; - float length = (float) sqrt ((r->x - (this->x1 + this->x2)/2) * (r->x - (this->x1 + this->x2)/2)+ (r->y - (this->y1 + this->y2)/2) * (r->y - (this->y1 + this->y2)/2)) ; - float line_tg = fabs ((float)((r->y - (this->y1 + this->y2)/2) / ((this->x1 + this->x2)/2 - r->x))) ; - //cout << " line_tg_prev = " << line_tg << "\n"; - float line_deg = atan (line_tg) * 180 / PI; - cout << "line_deg_prev = " << line_deg <<"\n"; - if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) { - if (r->y <= (this->y1 + this->y2)/2) - line_deg = this->deg + line_deg; - else - line_deg = fabs (line_deg - this->deg); - } - else { - if (r->y >= (this->y1 + this->y2)/2) - line_deg = this->deg + line_deg; - else - line_deg = fabs (line_deg - this->deg); - } - - cout << "line_deg = " << line_deg << "\n"; - line_tg = tan (line_deg * PI / 180); - - //cout << " line_tg = " << line_tg << "\n"; - float a = length * cos (atan (line_tg)); - //cout << " a = " << a << "\n"; - float b = (float) (this->f * a / (a + this->f)); - //cout << " b = " << b << "\n"; - c = sqrt ((p->x - (this->x1 + this->x2)/2) * (p->x - (this->x1 + this->x2)/2)+ (p->y - (this->y1 + this->y2)/2) * (p->y - (this->y1 + this->y2)/2)) ; - - if (((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) || ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), p->x, p->y,this->deg) >= 0))) { // точка пересечения и координата луча по разные стороны от опт.оси - l_1 = c + (float) b * line_tg; - cout << "here \n"; - alpha = atan ((float) (l_1 / b)) * 180 / PI; - alpha_r = 180 -alpha ; - if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + this->l / 2/cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2/cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) { //точка координата луча выше точка пересечения ниже - alpha_r = alpha_r + 2 * alpha; - alpha = 360 - alpha; - cout << " here_0 \n"; - } - - } - else { //точка персечкения и полоожение луча по одну сторону от опт.оси - l_1 = fabs (c - (float) b * line_tg); - alpha = atan ((float) (l_1 / b)) * 180 / PI; - alpha_r = 180 -alpha ; - cout << "here_here \n"; - if ((orient (this->x1, this->y1 + this->l / 2/cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) >= 0) && (orient (this->x1, this->y1 + (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)){ // луч выше опт.оси точка пересечения под всмогат.линией сверху - alpha_r = alpha_r + 2 * alpha; - alpha = 360 - alpha; - cout << "here_1 \n"; - - } - - - if ((orient (this->x1, this->y1 + this->l / 2 /cos (this->deg * PI / 180),this->x2, this->y2 - this->l / 2 /cos (this->deg * PI / 180), r->x, r->y,this->deg) <= 0) && (orient (this->x1, this->y1 + (this->l / 2 + b * line_tg) / cos (this->deg * PI / 180) ,this->x2, this->y2 - (this->l / 2 - b * line_tg) / cos (this->deg * PI / 180), p->x, p->y,this->deg) <= 0)) { // луч ниже опт.оси точка пересечения над всмогат.линией снизу - alpha_r = alpha_r + 2 * alpha; - alpha = 360 - alpha; - cout << "alpha =" << alpha << "\n"; - cout << "here_2 \n"; - } - - } - if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) - r->deg = alpha ; - else - r->deg = alpha_r; - - if (orient (this->x1,this->y1,this->x2,this->y2,r->x,r->y,1) < 0) { - if ((r->deg + this->deg - 360) > 0) - r->deg = (r->deg + this->deg - 360); - else - r->deg = r->deg + this->deg; - } - else { - - r->deg = r->deg + this->deg; - } - - r->x = p->x; - r->y = p->y; - } - const char *getID() const{ - return "Lens_ras"; - } - - ~Lens_ras () - { - cout << "Destructure of the Lens_ras" << "\n"; - } - - -}; -*/ - - class Lens : public Device{ public: float deg, f, l; @@ -1597,15 +1456,16 @@ class Prism : public Device { } } + int getID(){ + return 5; + } - const char *getID() const { - return "Prism"; - } +// const char *getID() const { +// return "Prism"; +// } ~Prism() { cout << "Destructure of the " << this->getID() << "\n"; } }; - - diff --git a/main/main.cc b/main/main.cc index a3290a0..7ae0895 100644 --- a/main/main.cc +++ b/main/main.cc @@ -237,6 +237,18 @@ for(int I=0; IgetID()==5){ + cross = my_device[num]->cross_point(my_laser_ray[I]); + sprintf(buf_, "%f %f %f %f %c", my_laser_ray[I]->x, my_laser_ray[I]->y, cross->x, cross->y, '\0');//new dot + if(send(cs, buf_, strlen(buf_)+1, MSG_NOSIGNAL)==-1){ + perror("Can't send:"); + return NULL; + } + recv(cs, temp, 1, 0); + float tx=cross->x; + float ty=cross->y; + my_device[num]->change_direction(my_laser_ray[I], cross); + } } else{ break;