A Circle and a Square

Ashish Patel
Codebrace
Published in
4 min readFeb 27, 2017

Problem Link: A Circle and a Square

In this challenge, you must implement part of a raster graphics editor that takes the coordinates of a circle and a square as input and draws them as filled-in shapes on a rectangular canvas.

The rectangular canvas consists of uniformly sized square pixels, and is w pixels wide, and h pixels high. Each point on the canvas belongs to a pixel, the intersection of two pixels has zero area, and each pixel is completely contained within the canvas.

The Cartesian coordinate system set up in the following way:

  • Point (0, 0) is the center of the top-left pixel of the canvas.
  • Point (w — 1, 0) is the center of the top-right pixel of the canvas.
  • Point (0 , h — 1) is the center of the bottom-left pixel of the canvas.
  • Point (w — 1, h — 1) is the center of the bottom-right pixel of the canvas.

Thus, all pixel centers have integer coordinates and if the center of a pixel has coordinates (xc, yc) then point (x, y) belongs to the pixel if and only if x ∈ [xc — 0.5, xc + 0.5] and y ∈ [yc — 0.5, yc + 0.5]. The two shapes should be drawn like so:

  • The circleis centered at the integer coordinates (xcircle, ycircle) and has non-negative integer radius r. A pixel should be black as a part of the circle if and only if the Euclidean distance from the pixel’s center to the center of the circle is not greater than .
  • The squareis defined by the integer coordinates of two of its opposite corners (x1, y1) and (x3, y3). A pixel should be black as a part of the square if and only if its center falls within the square or along its border. The coordinates of different corners of the square do not coincide.

Given h, w, and the definition of the circle and the square, print a raster image of the canvas where each character is either a . (denoting a white pixel outside of both shapes) or a #(denoting a black pixel that’s part of a shape).

Note: The first pixel of the first line of output should correspond to the top-left corner of the canvas.

Input Format

The first line contains two space-separated integers describing the respective values of w (canvas width) and h (canvas height).
The second line contains three space-separated integers describing the respective values of xcircle, ycircle, and r defining a circle with radius r centered at (xcircle, ycircle).
The third line contains four space-separated integers describing the respective values of x1, y1, x3, y3 defining a square with opposite corners at (x1, y1) and (x3,y3).

Constraints

  • 10 ≤ w, h ≤ 100
  • -100 ≤ xcircle, ycircle, x1, y1, x3, y3 ≤ 200
  • 0 ≤ r ≤ 100

Output Format

Print h lines where each line contains w characters. Each character must be either a . (to denote a white pixel) or a # (to denote a black pixel). The first pixel of the first line of output corresponds to the top-left corner of the canvas.

Sample Input 0

20 16
9 6 5
16 14 8 14

Sample Output 0

....................
.........#..........
......#######.......
.....#########......
.....#########......
.....#########......
....###########.....
.....#########......
.....#########......
.....#########......
......#######.......
.........#.###......
..........#####.....
.........#######....
........#########...
.........#######....

Explanation 0

The canvas has h= 16 rows and w =20 columns. The circle has radius r = 5 and is centered at point (9, 6) . The square has opposite corners located at points (16, 14) and (8, 14) and, as you can see, is rotated at an angle with its third corner at point (12, 10) (note that its fourth corner is outside the canvas boundary). In addition, the circle and the square overlap at point (12, 10).

SOLUTION

This problem is based on the coordinate geometry.
Now there are two cases either a point is on in the circle or point is on or inside the square.
Now to check if a point is on or inside the circle simply check its euclidean distance less if it is less than or equal to r or not.
Now for the square is a tricky part. Do the following step.
1. Find the other two coordinates of vertex of the square which is

Now to check if a point is inside a square or not so simply join the point with all the vertices of circle and find the area of these four triangles formed. If this area is equal to the area of the square then it is inside the square.

Now simply check of each point and print the . or # on the basis of the result we get in the above steps.

CODE

#include
using namespace std;
#define ll long long
int xc,yc,r;
double x1,y1,x2,y2,x3,y3,x4,y4;
double abs(double x)
{
if(x < 0)
return 0- x;
return x;
}
bool insideSquare(int i,int j)
{
double area1 = abs(i*(y1-y2) + x1*(y2-j) + x2*(j-y1)); // Triangle 1
double area2 = abs(i*(y2-y3) + x2*(y3-j) + x3*(j-y2)); // Triangle 2
double area3 = abs(i*(y3-y4) + x3*(y4-j) + x4*(j-y3)); // Triangle 3
double area4 = abs(i*(y4-y1) + x4*(y1-j) + x1*(j-y4)); // Triangle 4
double total_area = (area1+area2+area3+area4)/2;
double a = x1-x2;
double b = y1-y2;
double areaSquare = a*a + b*b; // area of square
return (total_area==areaSquare);
}
bool insideCircle(int i, int j)
{
int a = i-yc;
int b = j-xc;
int dist = a*a +b*b;
if(dist<= r*r) return true; return false; } int main() { int w,h; cin>>w>>h;
cin>>xc>>yc>>r;
cin>>x1>>y1>>x3>>y3;
// calculation of other two coordinate of square
x2 = (x1+x3+y3-y1)/2,y2=(y1+y3+x1-x3)/2,x4=(x1+x3+y1-y3)/2,y4=(y1+y3+x3-x1)/2;
for(int i=0;i<h;i++)
{
for(int j=0;j<w;j++)
{
// checking if the point is either in circle or square
if(insideSquare(j,i)== true || insideCircle(i,j)==true)
cout<<"#";
else
cout<<".";
}
cout<<endl;
}
}

--

--

Ashish Patel
Codebrace

Big Data Engineer at Skyscanner , loves Competitive programming, Big Data.