I need help with a problem I'm working on for a project with a robot. (Not homework - I don't get homework this hard) I'm using leJos API for a lego NXT robot. That means it dose have limited memory and slow computing.
Basically the has to measure the area of a triangle. I would like it to do this by running over the triangle and gathering points on the perimeter.
What I need is the following program:
Input:
Set of points (objects defined by x,y cords in 2D plane) that approximately fall on edge of a right triangle.
Output:
Area of best fitting right triangle
My current code is messy, but I think I'm on the right track.
Any help is greatly appreciated. This is a fun challenge- but Ive spent enough time on it- I need help. If more clarification is required I will definitely do my best to make it easy to help me
/>
Here is my main class. It just uses some predefined points to test the algorithm
Simple enough, a point.
The line class is important. It includes a static member class which holds the data from performing linear regression (the statistics algorithm for finding the best fit line from points)
Triangle.java is were it trys making a bunch of lines using the linier reggression in line.java and trys to find the best triangle
Basically the has to measure the area of a triangle. I would like it to do this by running over the triangle and gathering points on the perimeter.
What I need is the following program:
Input:
Set of points (objects defined by x,y cords in 2D plane) that approximately fall on edge of a right triangle.
Output:
Area of best fitting right triangle
My current code is messy, but I think I'm on the right track.
Any help is greatly appreciated. This is a fun challenge- but Ive spent enough time on it- I need help. If more clarification is required I will definitely do my best to make it easy to help me
package dev.robots.triangleProblem;
import java.util.Vector;
/*
* The Triangle Problem!
*
* Given: Right Triangle A exists in 2D space
*
* In: Points on parimeter of A, with at least 3 on each edge
*
* Out: Area of triangle
*/
public class Main {
public static void main(String[] args) {
Vector < Point> points = new Vector<Point>();
points.add( new Point(-2,-2));
points.add( new Point(3,-1));
points.add( new Point(1,3));
points.add( new Point(-0.5,-0.5));
points.add( new Point(0.5,2));
points.add( new Point(2,1));
points.add( new Point(1.5,2));
points.add( new Point(0,-1.5));
points.add( new Point(2,-0.25));
points.add( new Point(-1,-1.75));
points.add( new Point(2.5,0));
new Triangle(points);
}
}
Here is my main class. It just uses some predefined points to test the algorithm
package dev.robots.triangleProblem;
public class Point {
public boolean equals(Point p){
return p.x == x && p.y == y;
}
double x;
double y;
public Point(double x, double y){
this.x = x;
this.y = y;
}
public String toString(){
return "(" + x + ", " + y + ")";
}
}
Simple enough, a point.
package dev.robots.triangleProblem;
import java.util.List;
public class Line {
private double a;
private double b;
//ax + b
public boolean equals(Line l){
return a == l.a && b == l.b;
}
Line(double a,double B)/>/>{
this.a = a;
this.b = b;
}
Point intersection(Line l2){
return new Point((l2.b-B)/>/>/(a-l2.a), a*(l2.b-B)/>/>/(a-l2.a) + B)/>/>;
}
public String toString(){
return a + "x + " + b;
}
//Linier Reggression Class
public static class LineReg{
private Line l = null;
//R^2 - between 0 and 1, 1 being perfect fit, 0 being no corralation
private double r2 = -1;
private int nPoints = 0;
private List<Point> points;
public LineReg(List<Point> a){
nPoints = a.size();
points = a;
// first pass: read in data, compute xbar and ybar
double sumx = 0.0, sumy = 0.0;
for(int n = 0; n < a.size(); n++) {
sumx += a.get(n).x;
sumy += a.get(n).y;
}
double xbar = sumx / a.size();
double ybar = sumy / a.size();
// second pass: compute summary statistics
double xxbar = 0.0, yybar = 0.0, xybar = 0.0;
for (int i = 0; i < a.size(); i++) {
xxbar += (a.get(i).x - xbar) * (a.get(i).x - xbar);
yybar += (a.get(i).y - ybar) * (a.get(i).y - ybar);
xybar += (a.get(i).x - xbar) * (a.get(i).y - ybar);
}
double beta1 = xybar / xxbar;
double beta0 = ybar - beta1 * xbar;
double ssr = 0.0; // sum of squares
for (int i = 0; i < a.size(); i++) {
double fit = beta1*a.get(i).x + beta0;
ssr += (fit - ybar) * (fit - ybar);
}
this.l = new Line(beta1,beta0);
this.r2 = ssr / yybar;
}
public List<Point> points(){
return points;
}
public Line bestFit(){
return l;
}
public double r2(){
return r2;
}
public boolean equals(LineReg l){
return points == l.points;
}
public double lineValue(){
if(nPoints <= 2)
return 0;
//Make lines with more points better
return nPoints*r2()*r2();
}
}
}
The line class is important. It includes a static member class which holds the data from performing linear regression (the statistics algorithm for finding the best fit line from points)
package dev.robots.triangleProblem;
import java.util.List;
import java.util.Vector;
import dev.robots.triangleProblem.Line.LineReg;
public class Triangle {
private LineReg bestLine = null;
public static double area(Point a, Point b, Point c){
System.out.println(a.toString());
System.out.println(b.toString());
System.out.println(c.toString());
return Math.abs(a.x*(b.y-c.y) + b.x*(c.y-a.y) + c.x*(a.y-b.y) )/2;
}
private List<Point> check(List<Point> points){
for(int i = 0; i < points.size(); i ++){
for(int j = i+2; j < points.size(); j++){
LineReg comp = new LineReg(points.subList(i, j));
if (bestLine == null || bestLine.lineValue() < comp.lineValue()){
bestLine = comp;
}
}
}
List<Point> ps = new Vector<Point>();
for(Point p : points)
if(!bestLine.points().contains(p))
ps.add(p);
System.out.println(bestLine.bestFit().toString());
return ps;
}
private LineReg l1 = null;
private LineReg l2 = null;
private LineReg l3 = null;
Triangle(List<Point> points) {
points = check(points);
l1 = bestLine;
points = check(points);
l2 = bestLine;
points = check(points);
l3 = bestLine;
System.out.println(area(l1.bestFit().intersection(l2.bestFit()), l2.bestFit().intersection(l3.bestFit()), l3.bestFit().intersection(l1.bestFit())));
}
}
Triangle.java is were it trys making a bunch of lines using the linier reggression in line.java and trys to find the best triangle