Example 1 : Suppose you have a string of 6 integers such as: 000340
These digits represent the three co-ordinates on the Cartesian plane ( 0 , 0 ) , ( 0 , 3 ) and ( 4 , 0 ) .
If lines are drawn to connect each of the points, you form a triangle. Your task is to calculate the area of this triangle (given its co-ordinates). Clearly the above triangle has an area of 6 units squared.
Example 2 :
Suppose you have a string of 6 integers such as: 556739
These digits represent three co-ordinates on the Cartesian plane ( 5 , 5 ) , ( 6 , 7 ) , ( 3 , 9 ) .
If lines are drawn to connect each of these points, you form a triangle. After doing the calculations, the area of the above triangle has an area of 4 units squared.
You are provided with a text file containing the co-ordinates for each triangle.
Find the triangle with the largest area. Report the value for the area to one decimal place
Details and Assumptions
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Great to see good old unix one liners
Just use shoelace formula, the rest is easy: A = 2 1 ∣ ( x a − x c ) ( y b − y a ) − ( x a − x b ) ( y c − y a ) ∣
A rough solution in python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
My Java solution below. Not the most elegant but may be easier to understand.
package main;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class TrianglePoints {
public static class Cood {
private int x, y;
public Cood(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
public static class Triangle {
private Cood a, b, c;
public Triangle(Cood a, Cood b, Cood c) {
this.a = a;
this.b = b;
this.c = c;
}
private double calcAB() {
return calcLength(a, b);
}
private double calcBC() {
return calcLength(b, c);
}
private double calcAC() {
return calcLength(a, c);
}
private static double calcLength(Cood a, Cood b) {
return Math.sqrt(Math.pow(a.getX() - b.getX(), 2) + Math.pow(a.getY() - b.getY(), 2));
}
public double calcArea() {
return calcArea(calcAB(), calcBC(), calcAC());
}
private static double calcArea(double a, double b, double c) {
double s = (a + b + c) / 2;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}
}
public static void main(String[] args) {
double maxA = 0;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(new File("/Users/justausername/anotherfile/TrianglePoints.txt")));
String line = br.readLine();
while (line != null) {
Triangle t = new Triangle(new Cood(line.charAt(0), line.charAt(1)),
new Cood(line.charAt(2), line.charAt(3)), new Cood(line.charAt(4), line.charAt(5)));
double cA = t.calcArea();
if (maxA < cA) {
maxA = cA;
}
line = br.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.printf("%.1f", maxA);
}
}
}
Hey Serena! To a fellow JAVA coder, this is a great object-oriented programming approach to this problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
This is a solution in python 3.4:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Any idea why this isn't working?
1 2 3 4 5 6 7 8 |
|
@Agnishom Chattopadhyay @Aditya Raut @Brock Brown
Log in to reply
We are looking for the maximum area not the sum.
Log in to reply
Oh my bad! Why don't I read questions properly.
My Java Solution:
import java.io.File;
import java.util.Scanner;
public class TriangleAreas {
public static void main (String args []){
try{
//read in the file
Scanner file = new Scanner (new File ("TrianglePoints.txt"));
double maxArea = 0;
while (file.hasNext()){
//read in each line of the file
String line = file.nextLine();
//find the x and y value for first co-ordinate
int x1 = Integer.parseInt(line.charAt(0)+"");
int y1 = Integer.parseInt(line.charAt(1)+"");
//find the x and y value for second co-ordinate
int x2 = Integer.parseInt(line.charAt(2)+"");
int y2 = Integer.parseInt(line.charAt(3)+"");
//find the x and y value for third co-ordinate
int x3 = Integer.parseInt(line.charAt(4)+"");
int y3 = Integer.parseInt(line.charAt(5)+"");
//use the distance formula to get side lengths
double length1 =
Math.sqrt(Math.pow((x1-x2), 2) + Math.pow((y1-y2), 2));
double length2 =
Math.sqrt(Math.pow((x3-x2), 2) + Math.pow((y3-y2), 2));
double length3 =
Math.sqrt(Math.pow((x3-x1), 2) + Math.pow((y3-y1), 2));
//use Heron's formula to calculate area
double s = (length1+length2+length3)/(2.0);
double area =
Math.sqrt((s*(s-length1)*(s-length2)*(s-length3)));
if (area > maxArea){
maxArea = area;
}
}
System.out.printf("%.1f",maxArea);
}catch (Exception e){
System.out.println(e);
}
}
}
The program returns 23.5 as the answer
Nice problem! I found it easier to put it in Excel, get the individual digits, and use the matrix area formula below.
If your triangle has vertices ( a , b ) , ( c , d ) , ( e , f ) then the area of the triangle is the absolute value of 2 ∣ ∣ ∣ ∣ ∣ ∣ a c e b d f 1 1 1 ∣ ∣ ∣ ∣ ∣ ∣
Log in to reply
Did nearly the same thing, just used the expanded formula. If the points are labeled ( A x , A y ) , ( B x , B y ) , and ( C x , C y ) :
A r e a = ∣ ∣ ∣ ∣ 2 A x ( B y − C y ) + B x ( C y − A y ) + C x ( A y − B y ) ∣ ∣ ∣ ∣
The maximum comes from the triangle at the points ( 7 , 8 ) , ( 1 , 3 ) , ( 8 , 1 ) .
Problem Loading...
Note Loading...
Set Loading...
Bash one-liner:
781381 23.5