На плоскости заданы множество точек и окружность радиусом R с центром в начале координат.Построить множество всех треугольников
Помогите, пожалуйста, разобраться с задачкой. Условие : На плоскости заданы множество точек и окружность радиусом R с центром в начале координат. Построить множество всех треугольников с вершинами в заданных точках, имеющих непустое пересечение с окружностью.
Вот что выводит в таком виде , почему он мне не выводит координаты в ответ я не понимаю
P.s если ошибки банальные и глупые извиняйте я только учусь :c
#include <iostream>
#include <cstdlib>
#include <clocale>
#include <ctime>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;
struct point {
double x, y;
};
struct trg {
point a, b, c;
};
const int nmax = 30;
bool peres(double r , trg t)
{
return ((sqrt(t.a.x) + sqrt(t.a.y) > r * r) && (sqrt(t.b.x) + sqrt(t.b.y) < r * r)) || ((sqrt(t.a.x) + sqrt(t.a.y) > r * r) && (sqrt(t.c.x) + sqrt(t.c.y) < r * r)) || ((sqrt(t.b.x) + sqrt(t.b.y) > r * r) && (sqrt(t.c.x) + sqrt(t.c.y) < r * r));
}
int main() {
setlocale(LC_ALL, "ru");
point t[nmax];
trg p[30];
int np, nv, n, i, k, j;
double r;
trg tr;
srand(time(0));
do {
cout << "Количество точек кратное 3 до " << nmax << " n=";
cin >> n;
} while (n < 2 || n>30);
for (i = 0; i < n; ++i) {
t[i].x = rand() % 30 - 10;
t[i].y = rand() % 30 - 10;
}
cout << "Введите радиус окружности\nR=";
cin >> r;
cout << "Координаты точек:\nX:";
for (i = 0; i < n; ++i)
cout << t[i].x << " ";
cout << "\nY:";
for (i = 0; i < n; ++i)
cout << t[i].y << " ";
nv = 0;
np = 0;
for (i = 1; i < n - 2; i++) {
for (j = i + 1; j < n - 1; j++) {
for (k = j + 1; k < n; k++) {
tr.a = t[i];
tr.b = t[j];
tr.c = t[k];
if (peres(r, tr)) {
np++;
cout <<"np=" << np;
}
}
}
}
if (np == 0) { cout << "нет треугольников пересекающихся с окружностью\n"; }
else {
cout << "Множество треугольников пересекающихся с окружностью:";
for (i = 0; i < np - 1; i++) {
cout << "[(" << p[i].a.x << ";" << p[i].a.y << ")" << "(" << p[i].b.x << ";" << p[i].b.y << ")" << "(" << p[i].c.x << ";" << p[i].c.y << ")]";
Ответы (1 шт):
Автор решения: Harry
→ Ссылка
Если не ошибся при составлении и решении квадратных уравнений, то примерно так...
inline double sq(double x) { return x*x; }
bool isSect(double x0, double y0, double x1, double y1, double r)
{
double dx = x0 - x1, dy = y0 - y1;
dx *= dx;
dy *= dy;
if (dx + dy == 0) return false;
double det = (dx+dy)*sq(r) - sq(x1*y0 - x0*y1);
if (det < 0) return false;
double t1 = x0*(x0-x1) + y0*(y0-y1);
double t2 = (t1 + sqrt(det))/(dx+dy);
t1 = (t1 - sqrt(det))/(dx+dy);
if (t1 >= 0 && t1 <= 1) return true;
if (t2 >= 0 && t2 <= 1) return true;
return false;
}
struct pnt
{
int x, y;
};
void out(pnt a, pnt b, pnt c)
{
cout << "[(" << a.x << "," << a.y
<< ")(" << b.x << "," << b.y
<< ")(" << c.x << "," << c.y << ")]\n";
}
int main()
{
srand(time(0));
vector<pnt> p;
unsigned int n;
double r;
cout << "Points count: ";
cin >> n;
cout << "Radius: ";
cin >> r;
for(unsigned int i = 0; i < n; ++i)
{
pnt t;
t.x = rand()%30 - 10;
t.y = rand()%30 - 10;
p.push_back(t);
cout << "(" << t.x << ", " << t.y << ")\n";
}
for(unsigned int i = 0; i < n; ++i)
{
pnt a = p[i];
for(unsigned int j = i+1; j < n; ++j)
{
pnt b = p[j];
bool sectab = isSect(a.x,a.y,b.x,b.y,r);
for(unsigned int k = j+1; k < n; ++k)
{
pnt c = p[k];
if (sectab) out(a,b,c);
else if (isSect(a.x,a.y,c.x,c.y,r)) out(a,b,c);
else if (isSect(b.x,b.y,c.x,c.y,r)) out(a,b,c);
}
}
}
}
