D
D
Danivar2020-12-01 21:34:25
C++ / C#
Danivar, 2020-12-01 21:34:25

How to find points inside a shape, outside a shape, and on a shape?

Given a figure half rectangle half rhombus. Given points x (-150: 150) and y (-250; 250). Find points inside the shape, outside the shape, and on the shape.

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <math.h>
using namespace std;

struct Point {
    int x, y;
};

struct Count {
    int k1 = 0, k2 = 0, k3 = 0;
};

void array_input(Point* t, int n)
{
    srand(static_cast<unsigned int>(time(0)));
    for (int i = 0; i < n; ++i) {
       t[i].x = rand() % 501 - 250;
        t[i].y = rand() % 301 - 150;
    }

    cout << endl;
}

void array_output(Point* t, int n)
{
    for (int i = 0; i < n; ++i) {
        cout << "x: " << t[i].x << "\t" << setw(4) << " y: " << t[i].y << endl;
    }
}

Count number_of_points(Point* t, int n)
{

    Count out;
    for (int i = 0; i < n; ++i) {
            
  
        
          if (t[i].x < 150 && abs(t[i].y) < 200 && 1.5 * abs(t[i].y) + abs(t[i].x) < 150){
            out.k1++;
        }
        
        	else if (abs(t[i].x) > 150|| abs(t[i].y) > 200 || 1.5 * abs(t[i].y) + abs(t[i].x) > 150) {
            out.k3++;
        }
    
    else {
      out.k2++;
    }
    }

    return out;
}

int main()
{
  setlocale(LC_ALL, "rus");
    int n = 10;
    Point t[n];
    Count out;

    array_input(t, n);
    array_output(t, n);
    out = number_of_points(t, n);
    cout <<"Внутfdри фигуры: " << out.k1 << endl;
    cout <<"На фигуре: " << out.k2 << endl;
    cout <<"За пределами фигуры: " << out.k3 << endl;

    return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mayton2019, 2020-12-02
@Danivar

All such problems are solved by calculating the orientation of a point on a plane relative to a vector (in this case, this is the side of a rhombus). The cross product sign gives us orientations. And then - a matter of technology. Check all sides and prove that the point is on one side (plus or minus sign - depends on the basis and coordinate system). It is easier to calculate it experimentally in 2 checks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question