Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <iomanip>
- using namespace std;
- struct POINT {
- double x;
- double y;
- };
- typedef POINT * PPOINT;
- struct LINE {
- double Ax;
- double By;
- double C;
- };
- typedef LINE * PLINE;
- struct minimum_distance {
- POINT ptr;
- LINE lin;
- double dist;
- };
- typedef minimum_distance MINDIST;
- double distance(const PLINE, const PPOINT);
- MINDIST min_distance(PLINE, const size_t, PPOINT, const size_t);
- int main () {
- wcout.imbue(locale("rus_rus.866"));
- wcout << L"\n Введите количество точек: ";
- size_t qpt;
- cin >> qpt;
- wcout << L" Введите значения точек:\n\n";
- PPOINT ppt = new POINT [qpt];
- for (size_t n = 0; n < qpt; n++) {
- cout << " x" << n + 1 << ": "; cin >> ppt[n].x;
- cout << " y" << n + 1 << ": "; cin >> ppt[n].y;
- cout << endl;
- }
- wcout << L"\n Введите количество линий: ";
- size_t qln;
- cin >> qln;
- wcout << L" Введите значения линий:\n\n";
- PLINE pln = new LINE [qln];
- for (size_t n = 0; n < qln; n++) {
- cout << " Ax" << n + 1 << ": "; cin >> pln[n].Ax;
- cout << " Ay" << n + 1 << ": "; cin >> pln[n].By;
- cout << " C" << n + 1 << ": "; cin >> pln[n].C;
- if (!pln[n].Ax && !pln[n].By) --n;
- cout << endl;
- }
- MINDIST md = min_distance(pln, qln, ppt, qpt);
- delete[] ppt;
- delete[] pln;
- streamsize dist = 14, coord = 3;
- wcout << L"\n Минимальное расстояние: "
- << fixed << setprecision(dist) << md.dist
- << L"\n между точкой с координатами x: "
- << fixed << setprecision(coord) << md.ptr.x << L", y: " << md.ptr.y
- << L"\n и прямой, заданной коэффициентами Ax: "
- << md.lin.Ax << L", By: " << md.lin.By << L", C: " << md.lin.C << endl;
- cin.get(); cin.get();
- return 0;
- }
- double distance(const LINE _ln, const POINT _pt) {
- return abs(_ln.Ax * _pt.x + _ln.By * _pt.y - _ln.C) / sqrt(pow(_ln.Ax, 2) + pow(_ln.By, 2));
- }
- MINDIST min_distance(PLINE _pln, const size_t _lines, PPOINT _ppt, const size_t _points) {
- double min = DBL_MAX, eps = 1e-15;
- bool flag = false;
- MINDIST md;
- for (size_t line = 0; line < _lines; line++) {
- for (size_t point = 0; point < _points; point++) {
- double temp = distance(_pln[line], _ppt[point]);
- if (min > temp) {
- md.dist = min = temp;
- md.lin = _pln[line];
- md.ptr = _ppt[point];
- if (min <= eps) {
- min = 0;
- flag = true;
- break;
- }
- }
- }
- if (flag) break;
- }
- return md;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement