Area of triangle formed by the axes of co-ordinates and a given straight line - GeeksforGeeks

18 Mar, 2021
Given a straight line with equation coefficients as a, b & c(ax + by + c = 0), the task is to find the area of the triangle formed by the axes of co-ordinates and this straight line.Examples:  
Input: a = -2, b = 4, c = 3
Output: 0.5625

Input: a = 4, b = 3, c = 12
Output: 6
 
Approach:  
Let PQ be the straight line having AB, the line segment between the axes. The equation is, ax + by + c = 0  so, in intercept form it can be expressed as, x/(-c/a) + y/(-c/b) = 1  So, the x-intercept = -c/a the y-intercept = -c/b  So, it is very clear now the base of the triangle AOB will be -c/a and the base of the triangle AOB will be -c/b  So, area of the triangle  
Let PQ be the straight line having AB, the line segment between the axes. The equation is, ax + by + c = 0  
so, in intercept form it can be expressed as, x/(-c/a) + y/(-c/b) = 1  
So, the x-intercept = -c/a the y-intercept = -c/b  
So, it is very clear now the base of the triangle AOB will be -c/a and the base of the triangle AOB will be -c/b  
So, area of the triangle  
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ program area of triangle// formed by the axes of co-ordinates// and a given straight line #include <bits/stdc++.h>using namespace std; // Function to find areadouble area(double a, double b, double c){    double d = fabs((c * c) / (2 * a * b));    return d;} // Driver codeint main(){    double a = -2, b = 4, c = 3;    cout << area(a, b, c);    return 0;}
// Java program area of triangle// formed by the axes of co-ordinates// and a given straight line import java.io.*; class GFG{ // Function to find areastatic double area(double a, double b, double c){    double d = Math.abs((c * c) / (2 * a * b));    return d;} // Driver codepublic static void main (String[] args){         double a = -2, b = 4, c = 3;    System.out.println(area(a, b, c));}} // This code is contributed by ajit.
# Python3 program area of triangle# formed by the axes of co-ordinates# and a given straight line # Function to find areadef area(a, b, c):     d = abs((c * c) / (2 * a * b))    return d # Driver codea = -2b = 4c = 3print(area(a, b, c)) # This code is contributed# by mohit kumar
// C# program area of triangle// formed by the axes of co-ordinates// and a given straight lineusing System; class GFG{     // Function to find areastatic double area(double a, double b, double c){    double d = Math.Abs((c * c) / (2 * a * b));    return d;} // Driver codestatic public void Main (){         double a = -2, b = 4, c = 3;    Console.WriteLine (area(a, b, c));}} // This code is contributed by akt_mit.
<?php// PHP program area of triangle// formed by the axes of co-ordinates// and a given straight line // Function to find areafunction area($a, $b, $c){    $d = abs(($c * $c) / (2 * $a * $b));    return $d;} // Driver code$a = -2;$b = 4;$c = 3; echo area($a, $b, $c); // This code is contributed by Ryuga?>
<script> // javascript program area of triangle// formed by the axes of co-ordinates// and a given straight line // Function to find areafunction area(a , b , c){    var d = Math.abs((c * c) / (2 * a * b));    return d;} // Driver code     var a = -2, b = 4, c = 3;document.write(area(a, b, c));  // This code is contributed by Amit Katiyar </script>
0.5625
 
ankthon
mohit kumar 29
jit_t
amit143katiyar
area-volume-programs
school-programming
triangle
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Modular multiplicative inverse
Fizz Buzz Implementation
Singular Value Decomposition (SVD)
Check if a number is Palindrome
Segment Tree | Set 1 (Sum of given range)
How to check if a given point lies inside or outside a polygon?
Program to multiply two matrices
Count ways to reach the n'th stair
Merge two sorted arrays with O(1) extra space