clasa punct si derivat clasa segment; operatii cu segmente: lungime segment, lungime
maxima a unui segment din cele n, ordonarea a n segmente, numarare cate segmente sunt intr-un
cadran, cate au un capat pe une din axe
Răspunsuri la întrebare
Program C++:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Point {
public:
float x, y;
Point(float _x = 0, float _y = 0) {
x = _x;
y = _y;
}
};
class Segment {
Point a, b;
public:
Segment(Point _a = Point(), Point _b = Point()) {
a = _a;
b = _b;
}
pair<const Point&, const Point&> get_segment() const {
return { a,b };
}
float get_size() const {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
};
bool less_comp_segment(const Segment& a, const Segment& b) {
return a.get_size() < b.get_size();
}
int nr_seg_cadr(vector<Segment>& vsg, int id_cadran) {
switch (id_cadran)
{
case 1:{
int contor = 0;
for (auto& sgm : vsg) {
auto coords = sgm.get_segment();
if (coords.first.x > 0 && coords.first.y > 0 && coords.second.x > 0 && coords.second.y > 0)
++contor;
}
return contor;
}
case 2: {
int contor = 0;
for (auto& sgm : vsg) {
auto coords = sgm.get_segment();
if (coords.first.x > 0 && coords.first.y < 0 && coords.second.x > 0 && coords.second.y < 0)
++contor;
}
return contor;
}
case 3:{
int contor = 0;
for (auto& sgm : vsg) {
auto coords = sgm.get_segment();
if (coords.first.x < 0 && coords.first.y < 0 && coords.second.x < 0 && coords.second.y < 0)
++contor;
}
return contor;
}
case 4: {
int contor = 0;
for (auto& sgm : vsg) {
auto coords = sgm.get_segment();
if (coords.first.x < 0 && coords.first.y > 0 && coords.second.x < 0 && coords.second.y > 0)
++contor;
}
return contor;
}
default:
return -1;
}
}
int nr_seg_ax(vector<Segment>& vsg) {
int contor = 0;
for (auto& sgm : vsg) {
auto coords = sgm.get_segment();
if (coords.first.x == 0 || coords.first.y == 0 || coords.second.x == 0 || coords.second.y == 0)
++contor;
}
return contor;
}
int main() {
int nrseg;
cout << "Nr segmente : ";
cin >> nrseg;
vector<Segment> vsg;
vsg.reserve(nrseg);
//Citire segmente
for (int i = 0; i < nrseg; i++) {
cout << "Introduceti segment (xa, ya, xb, yb) : ";
float t1, t2, t3, t4;
cin >> t1 >> t2 >> t3 >> t4;
vsg.emplace_back(Point(t1,t2), Point(t3,t4));
}
//Sortare
sort(vsg.begin(), vsg.end(), less_comp_segment);
//Afisare dupa sorare:
cout << "\n\nSegmente sortate crecator dupa lungime : ";
for (auto& sgm : vsg) {
auto coords = sgm.get_segment();
cout << "\nSegment : ( (" << coords.first.x << "," << coords.first.y << ") , ( " << coords.second.x << "," << coords.second.y << ") )\t-> lungime : " << sgm.get_size();
}
//Afisare maxim :
auto coords_longest = vsg.back().get_segment();
cout << "\n\nCel mai lung segment este ( (" << coords_longest.first.x << "," << coords_longest.first.y << ") , ( " << coords_longest.second.x << "," << coords_longest.second.y << ") )\t-> lungime : " << vsg.back().get_size();
//Afisare nr segmente cadran
cout << "\n\nCadran I : " << nr_seg_cadr(vsg, 1);
cout << "\nCadran II : " << nr_seg_cadr(vsg, 2);
cout << "\nCadran III : " << nr_seg_cadr(vsg, 3);
cout << "\nCadran IV : " << nr_seg_cadr(vsg, 4);
//Afisare nr segmente capat pe axa
cout << "\n\nNr segmente capat axa : " << nr_seg_ax(vsg);
}