# Trouble calculating length and an angle

Page 1 of 1

## 2 Replies - 157 Views - Last Post: 29 May 2019 - 02:44 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=416288&amp;s=28c6695b8561c46529bd9899b3abefd8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 darkace79

Reputation: 1
• Posts: 51
• Joined: 03-October 13

# Trouble calculating length and an angle

Posted 29 May 2019 - 10:29 AM

I am trying to find the length of edges and then I try to find an angle. The edges always seem to be the same length even though they have different points. Trying to compare lengths of an Edge which the edge points are stored in an array. Seem to be going in circles here.

....

I found my issue.

This post has been edited by darkace79: 29 May 2019 - 12:20 PM

Is This A Good Question/Topic? 0

## Replies To: Trouble calculating length and an angle

### #2 NormR

• D.I.C Lover

Reputation: 775
• Posts: 5,836
• Joined: 25-December 13

## Re: Trouble calculating length and an angle

Posted 29 May 2019 - 10:55 AM

Can you explain the algorithm in English that you are trying to implement in code?
Post some example equations that show what you are trying to calculate.

What is the definition/declaration for the variable: edges?

This post has been edited by NormR: 29 May 2019 - 11:46 AM

### #3 NormR

• D.I.C Lover

Reputation: 775
• Posts: 5,836
• Joined: 25-December 13

## Re: Trouble calculating length and an angle

Posted 29 May 2019 - 02:44 PM

The posted code:

```       public double getAngleBetween(Edge edge2) {
Vector2d v1 = getVector();
Vector2d v2 = edge2.getVector();

return v1.angle(v2) * 180 / Math.PI;
}

private Vector2d getVector() {
Vector2d vec = new Vector2d(end.getX() - start.getX(), end.getY() - start.getY());
vec.normalize();
return vec;
}

public double getLength() {
double x = Math.abs(end.getX() - start.getX());
x *= x;
double y = end.getY() - start.getY();
y *= y;
double length = Math.sqrt(x + y);
return length;
}

angleBetween = edges[0].getAngleBetween(edges[3]);

double edge1 = edges[0].getLength();
double edge2 = edges[1].getLength();
double edge3 = edges[2].getLength();
double edge4 = edges[3].getLength();
double tol = 0.001;

if ((Math.abs(edge1 - edge2) <= tol) && (Math.abs(edge2 - edge3) <= tol)
&& (Math.abs(edge3 - edge4) <= tol) && (Math.abs(edge4 - edge1) <= tol) && angleBetween == 90) {
shape = "square";
} else if ((Math.abs(edge1 - edge3) <= tol) && (Math.abs(edge2 - edge4) <= tol) && angleBetween == 90) {
shape = "rectangle";
}else