1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
const int LARGE_NUM=1.0E10;
struct Point
{
public:
double x,y;
};
struct Line_seg
{
public:
Point endpt1, endpt2;
double slope, intercept;
};
class Boundary_map
{
private:
enum {NUM_MAP_PTS=10};
Point boundary_pts[NUM_MAP_PTS+1];
public:
Boundary_map();
void read_boundary_pts();
void map_wrap_around();
Point* get_boundary_pts();
int get_num_boundary_pts();
};
class Probe
{
private:
Point sample_pt, endpt1, endpt2;
int num_crosses;
Point probe_line_intersect (Line_seg);
int probe_line_seg_intersect (Point, Line_seg);
public:
Probe();
void read_sample_pt();
void check_map_boundary (Point*, int);
};
Boundary_map::Boundary_map()
{
int i;
for(i=0; i <NUM_MAP_PTS+1; i++)
{
boundary_pts[i].x=0;
boundary_pts[i].y=0;
}
}
void Boundary_map::read_boundary_pts()
{
int i;
ifstream infile ("C:\\BNDARY.DAT");
for(i=0; i<NUM_MAP_PTS; i++)
{
infile>>boundary_pts[i].x;
infile>>boundary_pts[i].y;
}
}
void Boundary_map::map_wrap_around()
{
boundary_pts[NUM_MAP_PTS]=boundary_pts[0];
}
Point* Boundary_map::get_boundary_pts();
{
return (boundary_pts);
}
int Boundary_map:: get_num_boundary_pts()
{
return (NUM_MAP_PTS+1);
}
Probe::Probe()
{
sample_pt.x=0;
sample_pt.y=0;
endpt1.x=0;
endpt1.y=0;
endpt2.x=LARGE_NUM;
endpt2.y=0;
num_crosses=0;
}
void Probe::read_sample_pt()
{
cout<<"Enter the x and y coordinates of the sample point"<<endl;
cin>>sample_pt.x>>sample_pt.y;
endpt1=sample_pt;
endpt2.y=sample_pt.y;
}
Point Probe::probe_line_intersect (Line_seg map_line_seg)
{
Point intersect_pt;
double probe_slope, probe_intercept;
if(fabs(map_line_seg.endpt2.x-map_line_seg.endpt1.x)<1.0E-20)
{
intersect_pt.x=map_line_seg.endpt1.x;
intersect_pt.y=endpt1.y;
}
else
{
map_line_seg.slope=(map_line_seg.slope=(map_line_seg.endpt2.y
-map_line_seg.endpt1.y)/(map_line_seg.endpt2.x
-map_line_seg.endpt1.x);
map_line_seg.intercept=map_line_seg.endpt2.y
-map_line_seg.slope*map_line_seg.endpt2.x;
probe_slope=0.0;
probe_intercept=sample_pt.y;
if(fabs(map_line_seg.slope)<1.0E-20)
{
intersect_pt.x=LARGE_NUM;
intersect_pt.y=LARGE_NUM;
}
intersect_pt.x=(probe_intercept-map_line_seg.intercept)/(map_line_seg.slope-probe_slope);
intersect_pt.y=map_line_seg.slope*intersect_pt.x+map_line_seg.intercept;
}
return (intersect_pt);
}
int Probe::probe_line_seg_intersect(Point intersect_pt, Line_seg seg2)
{
Line_seg seg1;
double x_low_1, x_high_1, x_low_2, x_high_2;
double y_low_1, y_high_1, y_low_2, y_high_2;
seg1.endpt1=endpt1;
seg1.endpt2=endpt2;
x_low_1=(seg1.endpt1.x<seg1.endpt2.x)?
seg1.endpt1.x:seg1.endpt2.x;
x_high_1=(seg1.endpt1.x>seg1.endpt2.x)?
seg1.endpt1.x:seg1.endpt2.x;
x_low_2=(seg2.endpt1.x<seg2.endpt2.x)?
seg2.endpt1.x:seg2.endpt2.x;
x_high_2=(seg2.endpt1.x>seg2.endpt2.x)?
seg2.endpt1.x:seg2.endpt2.x;
y_low_1=(seg1.endpt1.y<seg1.endpt2.y)?
seg1.endpt1.y:seg1.endpt2.y;
y_high_1=(seg1.endpt1.y>seg1.endpt2.y)?
seg1.endpt1.y:seg1.endpt2.y;
y_low_2=(seg2.endpt1.y<seg2.endpt2.y)?
seg2.endpt1.y:seg2.endpt2.y;
y_high_2=(seg2.endpt1.y>seg2.endpt2.y)?
seg2.endpt1.y:seg2.endpt2.y;
if((x_low_1<=intersect_pt.x && intersect_pt.x<=x_high_1) && (x_low_2<=intersect_pt.x &&
intersect_pt.x<=x_high_2) && (y_low_1<= intersect_pt.y && intersect_pt.y<=y_high_1) &&
(y_low_2<=intersect_pt.y && intersect_pt.y<=y_high_2))
{
return(1);
}
else
{
return(0);
}
}
void Probe:: check_map_boundary(Point* bnd_pt, int num_pts)
{
int i, does_cross, even_odd;
Point intersect_pt;
Line_seg map_line_seg;
for(i=0;i<num_pts-1;i++)
{
map_line_seg.endpt1=bnd_pt[i];
map_line_seg.endpt2=bnd_pt[i+1];
intersect_pt=probe_line_intersect(map_line_seg);
does_cross=probe_line_seg_intersect(intersect_pt, map_line_seg);
num_crosses+=does_cross;
}
even_odd=num_crosses % 2;
if(even_odd==0) cout << "The sample point is outside the " "plume. " <<endl;
if(even_odd==1) cout << "The sample point is inside the " "plume. " <<endl;
}
int main ()
{
Boundary_map plume_map;
Probe probe_from_water_sample;
plume_map.read_boundary_pts();
plume_map.map_wrap_around();
probe_from_water_sample.read_sample_pt();
probe_from_water_sample.check_map_boundary
(plume_map.get_boundary_pts(),
plume_map.get_num_boundary_pts());
}
|