Here is the code that I have been given for an assignment. The modification that I need to make is to allow the program to read 10 sample points and determine which are contained in the plume boundary. I have included the modification that I think is appropriate for the requirement but I just want to check if this looks right. Also any suggestions on how to better accomplish the requirement would be greatly appreciated.
Don't mind the k variable and cout statement in the function
void Boundary_map::read_boundary_pts(). It is just a test to make sure the input file is being read.
// UNDERGROUND POLLUTION PLUME BOUNDARIES
#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,k;
ifstream infile ("C:\\BNDARY.TXT");
k = infile.eof();
cout<<k<<endl;
for (i = 0; i < NUM_MAP_PTS + 1; i++)
{
infile >> boundary_pts[i].x;
infile >> boundary_pts[i].y;
}
k = infile.eof();
cout<<k<<endl;
}
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.endpt2.y
- map_line_seg.endpt1.y) / (map_line_seg.endpt2.x
- map_line_seg.endpt2.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());
system("pause");
}