#include #include #include #include #include "cgi.h" #include "sailselection.h" using namespace std; int main() { cout << "Content-type: text/html\n\n" << "\n" << "\n" << "\n" << "True Wind & Sail Recommendation\n" << "\n" << "\n" << "

True Wind & Sail Recommendation

\n"; const cgi c; const map& m {c.get_input()}; auto get_double = [&](const string& key) -> double { auto it {m.find(key)}; if (it == end(m) || it->second.empty()) return 0.0; return stod(it->second); }; const double aws {get_double("aws")}; const double awa {get_double("awa")}; const double boat_speed {get_double("boat_speed")}; const double current_speed {get_double("current_speed")}; const double current_angle {get_double("current_angle")}; // validate inputs bool valid {true}; if (aws < 0.0) { cout << "

Apparent wind speed must be non-negative.

\n"; valid = false; } if (awa < -180.0 || awa > 180.0) { cout << "

Apparent wind angle must be between -180 and 180.

\n"; valid = false; } if (boat_speed < 0.0) { cout << "

Boat speed must be non-negative.

\n"; valid = false; } if (valid) { const double pi {acos(-1.0)}; const double awa_r {awa * pi / 180.0}; const double cur_r {current_angle * pi / 180.0}; // apparent wind vector const double aw_x {aws * sin(awa_r)}; const double aw_y {aws * cos(awa_r)}; // current vector const double cur_x {current_speed * sin(cur_r)}; const double cur_y {current_speed * cos(cur_r)}; // true wind = apparent wind + boat velocity - current const double tw_x {aw_x + 0.0 - cur_x}; const double tw_y {aw_y - boat_speed - cur_y}; // convert back to speed and angle const double tws {sqrt(tw_x * tw_x + tw_y * tw_y)}; const double twa_r {atan2(tw_x, tw_y)}; const double twa_deg {twa_r * 180.0 / pi}; const double abs_twa {abs(twa_deg)}; const string tack {twa_deg < 0.0 ? "port" : "starboard"}; cout << "

True wind speed: " << tws << " knots

\n" << "

True wind angle: " << abs_twa << "° " << tack << "

\n"; // bear away / head up checks const SSC ssc; const vector recs {ssc.lookup(tws, abs_twa)}; if (recs.empty()) { if (abs_twa < 90.0) { cout << "

Sail recommendation: Bear Away

\n"; } else { cout << "

Sail recommendation: Head Up

\n"; } } else { cout << "

Sail Recommendation

\n"; for (const sail_rec& r : recs) { cout << r; } } } cout << "

Return

\n"; cout << "\n" << "\n"; return EXIT_SUCCESS; }