Find out if a point is in a square / rectangle

Title Description:

Given the coordinates x,y and side length a of the square, then the diagonal of the square is x,y and x+a,y+a respectively. Given a coordinate, ask if the coordinate is in the square (also on the boundary)

Input:

3 2 8
8 10

Output:

yes

Write simple functions:

bool IsinRange(int a, int Num, int b) {
	return a <= Num && Num <= b;
}

Topic expansion:

What if it's a rectangle?
2 if you give a rectangle (top left and bottom right) (bottom left and top right)

#include<iostream>

using namespace std;

int square[55];
const int minint = -100000000;
int lx[55], ly[55], rx[55], ry[55];
bool insquare[55];

bool IsinRange(int a, int Num, int b) {
	return a <= Num && Num <= b;
}
bool legal(int a, int Num, int b) {
	if (a>b)
	{
		return IsinRange(b, Num, a);
	}
	else
	{
		return IsinRange(a, Num, b);
	}
}
int Getlength(int x1, int x2) {
	if (x1>x2)
	{
		return x1 - x2;
	}
	else
	{
		return x2 - x1;
	}
}
//Find area
int Getsquare(int x1, int y1, int x2, int y2) {
	int x, y;
	x = Getlength(x1, x2);
	y = Getlength(y1, y2);
	return x * y;
}

int main() 
{
	int i = 0, j = 0,k = 0;
	int n;
	int gx, gy;
	cin >> n;
	for ( i = 0; i <n ; i++)
	{
		cin >> lx[i] >> ly[i] >> rx[i] >> ry[i];
	}
	for ( i = 0; i < n; i++)
	{
		square[i] = Getsquare(lx[i], ly[i], rx[i], ry[i]);
	}
	for ( i = 0; i < n; i++)
	{
		cin >> gx >> gy;
	}
	for ( i = 0; i < n; i++)
	{
		if (legal(lx[i],gx,rx[i])&&legal(ly[i],gx,ry[i]))
		{
			insquare[i] = true;
		}
	}

	return 0;
}

Example:
Title Description

There are several conductive rectangles on the map with different lengths (coordinates x, y < = 10000 of four corners of the rectangles). Moreover, the electrician thinks that the rectangles are standard anyway, and the sides are parallel to the coordinate axis, so it's enough to give the rectangles to any two diagonal points The electrician thinks you're smart enough.
There is an electric needle at a certain position, and a current is discharged. Only the rectangle containing (including on the boundary) the electric needle can guide the current, and the rectangle with the largest final area is the rectangle with the diversion.
Find out that this rectangle is the number of rectangles given in the input data

input

The first line is a number n, indicating the number of rectangles
Next n lines,
Four integers in each line, x1,y1,x2,y2, represent the coordinates of the diagonal of rectangle i
Next 1 line, two integers x, y, represent the coordinates of the electric needle


output

An integer that represents the number of the rectangle of the diversion (if the areas are equal, the number should be as small as possible)

sample input

3
6 8 2 3
1 3 5 6
8 2 1 9
4 4



sample output

3

#include<iostream>

using namespace std;

int square[55];
const int minint = -100000000;
int lx[55], ly[55], rx[55], ry[55];
bool insquare[55];

bool IsinRange(int a, int Num, int b) {
	return a <= Num && Num <= b;
}
bool legal(int a, int Num, int b) {
	if (a>b)
	{
		return IsinRange(b, Num, a);
	}
	else
	{
		return IsinRange(a, Num, b);
	}
}
int Getlength(int x1, int x2) {
	if (x1>x2)
	{
		return x1 - x2;
	}
	else
	{
		return x2 - x1;
	}
}
//Find area
int Getsquare(int x1, int y1, int x2, int y2) {
	int x, y;
	x = Getlength(x1, x2);
	y = Getlength(y1, y2);
	return x * y;
}

int main() 
{
	int i = 0, j = 0,k = 0;
	int n;
	int gx, gy;
	int a;
	cin >> n; //n rectangles
	//Read in the coordinates of n rectangular points
	for ( i = 0; i < n ; i++)
	{
		cin >> lx[i] >> ly[i] >> rx[i] >> ry[i];
	}
	for ( i = 0; i < n; i++)
	{
		square[i] = Getsquare(lx[i], ly[i], rx[i], ry[i]);
	}
	//Reading in needle coordinates
	cin >> gx >> gy;

	for ( i = 0; i < n; i++)
	{
		if (legal(lx[i],gx,rx[i])&&legal(ly[i],gx,ry[i]))
		{
			insquare[i] = true;
		}
	}

	a = 0;
	int maxsquare = minint;
	for (i = 0; i < n; i++)
	{
		if (maxsquare < square[i]) //Compare sizes
		{
			if (insquare[i]) // Judge legitimacy
			{
				maxsquare = square[i];
				a = i;
			}
		}
	}
	cout << a+1 << endl;
	return 0;
}


Posted on Mon, 04 May 2020 20:17:22 -0700 by premiso