Find two vertices of an isosceles triangle in which there is rectangle with opposite corners (0, 0) and (X, Y)
Given two integers X and Y. The task is to find two vertices of an isosceles triangle ABC(right-angled at B) which has one vertex at a point B(0, 0). And there is a rectangle with opposite sides (0, 0) and (X, Y). All the points of this rectangle are located inside or on the border of the triangle. Print 4 integers x1, y1, x2, y2, where A(x1, y1) and B(x2, y2).
Examples:
Input : X = 3, Y = 3
Output : 6 0 0 6
Input : X = -3, y = -2
Output : -5 0 0 -5
Approach :
Let Val = |x| + |y|. Then the first point is (Val * sign(x), 0) and the second point is (0, Val * sign(y)).
Let’s see how it works for x > 0 and y > 0. Other cases can be proved in a similar way.
We need to show, that (x, y) belongs to our triangle(including its borders). In fact (x, y) belongs to segment, connecting (x + y, 0) with (0, x + y). The line through (x + y, 0) and (0, x + y) is Y = – X + x + y. Using coordinates (x, y) in this equation proves our answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Vertices( int x, int y)
{
int val = abs (x) + abs (y);
cout << val * (x < 0 ? -1 : 1) << " 0 " ;
cout << "0 " << val * (y < 0 ? -1 : 1);
}
int main()
{
int x = 3, y = 3;
Vertices(x, y);
return 0;
}
|
Java
class GFG
{
static void Vertices( int x, int y)
{
int val = Math.abs(x) + Math.abs(y);
System.out.print(val * (x < 0 ? - 1 : 1 ) + " 0 " );
System.out.print( "0 " + val * (y < 0 ? - 1 : 1 ));
}
public static void main(String[] args)
{
int x = 3 , y = 3 ;
Vertices(x, y);
}
}
|
Python3
def Vertices(x, y) :
val = abs (x) + abs (y);
if x < 0 :
x = - 1
else :
x = 1
print (val * x, "0" ,end = " " );
if y < 0 :
y = - 1
else :
y = 1
print ( "0" ,val * y);
if __name__ = = "__main__" :
x = 3 ; y = 3 ;
Vertices(x, y);
|
C#
using System;
class GFG
{
static void Vertices( int x, int y)
{
int val = Math.Abs(x) + Math.Abs(y);
Console.Write(val * (x < 0 ? -1 : 1) + " 0 " );
Console.Write( "0 " + val * (y < 0 ? -1 : 1));
}
public static void Main(String[] args)
{
int x = 3, y = 3;
Vertices(x, y);
}
}
|
Javascript
<script>
function Vertices(x, y)
{
let val = Math.abs(x) + Math.abs(y);
document.write(val * (x < 0 ? -1 : 1) + " 0 " );
document.write( "0 " + val * (y < 0 ? -1 : 1));
}
let x = 3, y = 3;
Vertices(x, y);
</script>
|
Time Complexity : O(1)
Auxiliary Space: O(1)
Last Updated :
31 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...