Find the coordinates of a triangle whose Area = (S / 2)

Given an integer S, the task is to find the coordinates of a triangle whose area is (S / 2).

Examples:

Input: S = 4
Output:
(0, 0)
(1000000000, 1)
(999999996, 1)

Input: S = 15
Output:
(0, 0)
(1000000000, 1)
(999999985, 1)

Approach:



  • It is know than the area of the triangle whose coordinates are (X1, Y1), (X2, Y2) and (X3, Y3) is given by A = ((X1 * Y2) + (X2 * Y3) + (X3 * Y1) – (X1 * Y3) – (X2 * Y1) – (X3 * Y2)) / 2.
  • Now fixing (X1, Y1) to (0, 0) gives A = ((X2 * Y3) – (X3 * Y2)) / 2.
  • It is given that A = S / 2 which implies S = (X2 * Y3) – (X3 * Y2).
  • Now fix (X2, Y2) to (109, 1) and the equation becomes S = 109 * Y3 – X3 which can be solved by taking an integer value of a variable that given the integer value for the other variable.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
const long MAX = 1000000000;
  
// Function to find the traingle
// with area = (S / 2)
void findTriangle(long S)
{
  
    // Fix the two pairs of coordinates
    long X1 = 0, Y1 = 0;
    long X2 = MAX, Y2 = 1;
  
    // Find (X3, Y3) with integer coordinates
    long X3 = (MAX - S % MAX) % MAX;
    long Y3 = (S + X3) / MAX;
  
    cout << "(" << X1 << ", " << Y1 << ")\n";
    cout << "(" << X2 << ", " << Y2 << ")\n";
    cout << "(" << X3 << ", " << Y3 << ")";
}
  
// Driver code
int main()
{
  
    long S = 4;
  
    findTriangle(S);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
class GFG
{
    static final long MAX = 1000000000
      
    // Function to find the traingle 
    // with area = (S / 2) 
    static void findTriangle(long S) 
    
      
        // Fix the two pairs of coordinates 
        long X1 = 0, Y1 = 0
        long X2 = MAX, Y2 = 1
      
        // Find (X3, Y3) with integer coordinates 
        long X3 = (MAX - S % MAX) % MAX; 
        long Y3 = (S + X3) / MAX; 
      
        System.out.println("(" + X1 + 
                           ", " + Y1 + ")"); 
        System.out.println("(" + X2 + 
                           ", " + Y2 + ")"); 
        System.out.println("(" + X3 + 
                           ", " + Y3 + ")"); 
    
      
    // Driver code 
    public static void main (String[] args) 
    
        long S = 4
      
        findTriangle(S); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
MAX = 1000000000
  
# Function to find the traingle 
# with area = (S / 2) 
def findTriangle(S) : 
  
    # Fix the two pairs of coordinates 
    X1 = 0; Y1 = 0
    X2 = MAX; Y2 = 1
  
    # Find (X3, Y3) with integer coordinates 
    X3 = (MAX - S % MAX) % MAX
    Y3 = (S + X3) / MAX
  
    print("(", X1, ",", Y1, ")"); 
    print("(", X2, ",", Y2, ")"); 
    print("(", X3, ",", Y3, ")"); 
  
# Driver code 
if __name__ == "__main__"
  
    S = 4
  
    findTriangle(S); 
  
# This code is contributed by kanugargng

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach 
using System;
      
class GFG
{
    static readonly long MAX = 1000000000; 
      
    // Function to find the traingle 
    // with area = (S / 2) 
    static void findTriangle(long S) 
    
      
        // Fix the two pairs of coordinates 
        long X1 = 0, Y1 = 0; 
        long X2 = MAX, Y2 = 1; 
      
        // Find (X3, Y3) with integer coordinates 
        long X3 = (MAX - S % MAX) % MAX; 
        long Y3 = (S + X3) / MAX; 
      
        Console.WriteLine("(" + X1 + 
                         ", " + Y1 + ")"); 
        Console.WriteLine("(" + X2 + 
                         ", " + Y2 + ")"); 
        Console.WriteLine("(" + X3 + 
                         ", " + Y3 + ")"); 
    
      
    // Driver code 
    public static void Main (String[] args) 
    
        long S = 4; 
      
        findTriangle(S); 
    
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Output:

(0, 0)
(1000000000, 1)
(999999996, 1)

Time Complexity: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.