info@meritcampus.com    +91-85006-22255
...

Given the list of triangles, write a program to find the triangle whose center (centroid) is nearest to the origin (0,0). When returning the triangle, the points of the triangle should be adjusted such that the top, left and bottom values represent the positions in the coordinate system. The top point is top most left point and left point is left most bottom point. The remaining point will be considered as the right point. Assume that input list contains only acute triangles.

Input (List) Output (Triangle)
`[{[0.0,0.0] - [2.0,0.0] - [1.0,3.0]},{[0.0,0.0] - [8.0,0.0] - [7.0,8.0]}]` `{[1.0,3.0] - [0.0,0.0] - [2.0,0.0]}`
`[{[0.0,0.0] - [2.0,0.0] - [1.0,3.0]}]` `{[1.0,3.0] - [0.0,0.0] - [2.0,0.0]}`
`[{[0.0,0.0] - [8.0,0.0] - [7.0,8.0]},{[5.0,5.0] - [13.0,5.0] - [12.0,13.0]},{[10.0,0.0] - [18.0,0.0] - [77.0,8.0]}]` `{[7.0,8.0] - [0.0,0.0] - [8.0,0.0]}`
`[{[0.0,0.0] - [2.0,0.0] - [1.0,90.0]},{[21.0,0.0] - [50.0,5.0] - [120.0,111.0]},{[0.0,0.0] - [28.0,0.0] - [7.0,8.0]},{[-10.0,-11.0] - [0.0,0.0] - [-5.0,0.0]}]` `{[-5.0,0.0] - [-10.0,-11.0] - [0.0,0.0]}`
EMPTY LIST null

``` class FindNearestTriangle {    public static void main(String s[])    {        List points = new ArrayList();        points.add(new Triangle(0, 0, 2, 0, 1, 90));        points.add(new Triangle(21, 0, 50, 5, 120, 111));        points.add(new Triangle(0, 0, 28, 0, 7, 8));        points.add(new Triangle(-10, -11, 0, 0, -5, 0));        System.out.println("The nearest triangle is : " + findNearestTriangle(points));    } public static Triangle findNearestTriangle(List<Triangle> triangles) { //Write code here to find the nearest triangle as per the requirements mentioned above } //If required write any additional methods here } class Point { double x; double y; Point(double x, double y) { this.x = x; this.y = y; } @Override public String toString() { return "[" + x + "," + y + "]"; } } class Triangle { Point top; Point left; Point right; public Triangle(Point top, Point left, Point right) { this.top = top; this.left = left; this.right = right; } public Triangle(int x1, int y1, int x2, int y2, int x3, int y3) { this(new Point(x1, y1), new Point(x2, y2), new Point(x3, y3)); } @Override public String toString() { return "{" + top + " - " + left + " - " + right + "}"; } } ```

Topic: Java ArrayList

0
Wrong
Score more than 2 points