Finding 4th vertex of a rectangle
I need my code to find the 4th vertex of a rectangle. Tried to make something but as always failed. It works in some cases, bot not everytime. Could someone help me with it?
Also, it has to be done with 2 classes, one for vertices and other one for a whole rectangle.
Problem is that my code works for a certain vertex setting. I was trying many things but still fail.
Here's the code:
#include <iostream>
#include <cmath>
using namespace std;
class vertex{
public:
double x;
double y;
void insert(){
cin>>x;
cin>>y;
}
};
class rect{
private:
double ax, ay, bx, by, cx, cy, dx, dy;
public:
void insert_data(vertex A, vertex B, vertex C){ //inserting rectangle data
ax=A.x;
ay=A.y;
bx=B.x;
by=B.y;
cx=C.x;
cy=C.y;
}
void calculate(){ //finding 4h vertex coordinates
dx=cx+(abs(ax-bx));
dy=cy+(abs(ay+by));
}
void out(){ //output
cout<<dx<<" "<<dy;
}
};
using namespace std;
int main() {
vertex A, B, C;
A.insert();
B.insert();
C.insert();
rect X;
X.insert_data(A, B, C);
X.calculate();
X.out();
return 0;
}
As I think about it now it may have something to do with inserting the coordinates to the right variables, but can't think of a solution to fix this.
For example:
Input:
1 1
0 3
3 2
Output:
2 4
Order of inputing each vertex is not specified.
c++
|
show 11 more comments
I need my code to find the 4th vertex of a rectangle. Tried to make something but as always failed. It works in some cases, bot not everytime. Could someone help me with it?
Also, it has to be done with 2 classes, one for vertices and other one for a whole rectangle.
Problem is that my code works for a certain vertex setting. I was trying many things but still fail.
Here's the code:
#include <iostream>
#include <cmath>
using namespace std;
class vertex{
public:
double x;
double y;
void insert(){
cin>>x;
cin>>y;
}
};
class rect{
private:
double ax, ay, bx, by, cx, cy, dx, dy;
public:
void insert_data(vertex A, vertex B, vertex C){ //inserting rectangle data
ax=A.x;
ay=A.y;
bx=B.x;
by=B.y;
cx=C.x;
cy=C.y;
}
void calculate(){ //finding 4h vertex coordinates
dx=cx+(abs(ax-bx));
dy=cy+(abs(ay+by));
}
void out(){ //output
cout<<dx<<" "<<dy;
}
};
using namespace std;
int main() {
vertex A, B, C;
A.insert();
B.insert();
C.insert();
rect X;
X.insert_data(A, B, C);
X.calculate();
X.out();
return 0;
}
As I think about it now it may have something to do with inserting the coordinates to the right variables, but can't think of a solution to fix this.
For example:
Input:
1 1
0 3
3 2
Output:
2 4
Order of inputing each vertex is not specified.
c++
Lets say I give you (0,0), (1,0), (0, -1) as A, B and C. Would calculate give you (1, -1)? Can you see the math that would?
– NathanOliver
Nov 19 '18 at 22:50
Always pass objects by reference, not by value!
– SilvioCro
Nov 19 '18 at 22:57
@NathanOliver It acually does if I input than in that specific order, but doesn't if I input it for example backwards. What could solve that problem?
– Krystian Niżeniec
Nov 19 '18 at 23:02
@SilvioCro Changed it, thanks! Our teacher didn't say that it's a must.
– Krystian Niżeniec
Nov 19 '18 at 23:04
1
You should use a dot product to check if A, B, C are correct rectangle coordinates. Then in vector form : DC = AB or D = C - (B - A).
– tunglt
Nov 19 '18 at 23:19
|
show 11 more comments
I need my code to find the 4th vertex of a rectangle. Tried to make something but as always failed. It works in some cases, bot not everytime. Could someone help me with it?
Also, it has to be done with 2 classes, one for vertices and other one for a whole rectangle.
Problem is that my code works for a certain vertex setting. I was trying many things but still fail.
Here's the code:
#include <iostream>
#include <cmath>
using namespace std;
class vertex{
public:
double x;
double y;
void insert(){
cin>>x;
cin>>y;
}
};
class rect{
private:
double ax, ay, bx, by, cx, cy, dx, dy;
public:
void insert_data(vertex A, vertex B, vertex C){ //inserting rectangle data
ax=A.x;
ay=A.y;
bx=B.x;
by=B.y;
cx=C.x;
cy=C.y;
}
void calculate(){ //finding 4h vertex coordinates
dx=cx+(abs(ax-bx));
dy=cy+(abs(ay+by));
}
void out(){ //output
cout<<dx<<" "<<dy;
}
};
using namespace std;
int main() {
vertex A, B, C;
A.insert();
B.insert();
C.insert();
rect X;
X.insert_data(A, B, C);
X.calculate();
X.out();
return 0;
}
As I think about it now it may have something to do with inserting the coordinates to the right variables, but can't think of a solution to fix this.
For example:
Input:
1 1
0 3
3 2
Output:
2 4
Order of inputing each vertex is not specified.
c++
I need my code to find the 4th vertex of a rectangle. Tried to make something but as always failed. It works in some cases, bot not everytime. Could someone help me with it?
Also, it has to be done with 2 classes, one for vertices and other one for a whole rectangle.
Problem is that my code works for a certain vertex setting. I was trying many things but still fail.
Here's the code:
#include <iostream>
#include <cmath>
using namespace std;
class vertex{
public:
double x;
double y;
void insert(){
cin>>x;
cin>>y;
}
};
class rect{
private:
double ax, ay, bx, by, cx, cy, dx, dy;
public:
void insert_data(vertex A, vertex B, vertex C){ //inserting rectangle data
ax=A.x;
ay=A.y;
bx=B.x;
by=B.y;
cx=C.x;
cy=C.y;
}
void calculate(){ //finding 4h vertex coordinates
dx=cx+(abs(ax-bx));
dy=cy+(abs(ay+by));
}
void out(){ //output
cout<<dx<<" "<<dy;
}
};
using namespace std;
int main() {
vertex A, B, C;
A.insert();
B.insert();
C.insert();
rect X;
X.insert_data(A, B, C);
X.calculate();
X.out();
return 0;
}
As I think about it now it may have something to do with inserting the coordinates to the right variables, but can't think of a solution to fix this.
For example:
Input:
1 1
0 3
3 2
Output:
2 4
Order of inputing each vertex is not specified.
c++
c++
edited Nov 19 '18 at 23:15
Krystian Niżeniec
asked Nov 19 '18 at 22:47
Krystian NiżeniecKrystian Niżeniec
285
285
Lets say I give you (0,0), (1,0), (0, -1) as A, B and C. Would calculate give you (1, -1)? Can you see the math that would?
– NathanOliver
Nov 19 '18 at 22:50
Always pass objects by reference, not by value!
– SilvioCro
Nov 19 '18 at 22:57
@NathanOliver It acually does if I input than in that specific order, but doesn't if I input it for example backwards. What could solve that problem?
– Krystian Niżeniec
Nov 19 '18 at 23:02
@SilvioCro Changed it, thanks! Our teacher didn't say that it's a must.
– Krystian Niżeniec
Nov 19 '18 at 23:04
1
You should use a dot product to check if A, B, C are correct rectangle coordinates. Then in vector form : DC = AB or D = C - (B - A).
– tunglt
Nov 19 '18 at 23:19
|
show 11 more comments
Lets say I give you (0,0), (1,0), (0, -1) as A, B and C. Would calculate give you (1, -1)? Can you see the math that would?
– NathanOliver
Nov 19 '18 at 22:50
Always pass objects by reference, not by value!
– SilvioCro
Nov 19 '18 at 22:57
@NathanOliver It acually does if I input than in that specific order, but doesn't if I input it for example backwards. What could solve that problem?
– Krystian Niżeniec
Nov 19 '18 at 23:02
@SilvioCro Changed it, thanks! Our teacher didn't say that it's a must.
– Krystian Niżeniec
Nov 19 '18 at 23:04
1
You should use a dot product to check if A, B, C are correct rectangle coordinates. Then in vector form : DC = AB or D = C - (B - A).
– tunglt
Nov 19 '18 at 23:19
Lets say I give you (0,0), (1,0), (0, -1) as A, B and C. Would calculate give you (1, -1)? Can you see the math that would?
– NathanOliver
Nov 19 '18 at 22:50
Lets say I give you (0,0), (1,0), (0, -1) as A, B and C. Would calculate give you (1, -1)? Can you see the math that would?
– NathanOliver
Nov 19 '18 at 22:50
Always pass objects by reference, not by value!
– SilvioCro
Nov 19 '18 at 22:57
Always pass objects by reference, not by value!
– SilvioCro
Nov 19 '18 at 22:57
@NathanOliver It acually does if I input than in that specific order, but doesn't if I input it for example backwards. What could solve that problem?
– Krystian Niżeniec
Nov 19 '18 at 23:02
@NathanOliver It acually does if I input than in that specific order, but doesn't if I input it for example backwards. What could solve that problem?
– Krystian Niżeniec
Nov 19 '18 at 23:02
@SilvioCro Changed it, thanks! Our teacher didn't say that it's a must.
– Krystian Niżeniec
Nov 19 '18 at 23:04
@SilvioCro Changed it, thanks! Our teacher didn't say that it's a must.
– Krystian Niżeniec
Nov 19 '18 at 23:04
1
1
You should use a dot product to check if A, B, C are correct rectangle coordinates. Then in vector form : DC = AB or D = C - (B - A).
– tunglt
Nov 19 '18 at 23:19
You should use a dot product to check if A, B, C are correct rectangle coordinates. Then in vector form : DC = AB or D = C - (B - A).
– tunglt
Nov 19 '18 at 23:19
|
show 11 more comments
2 Answers
2
active
oldest
votes
If you have three vertices, then you have half of the rectangle: a right triangle. First you need to determine which point is at the right angle. You can do this in different ways. A way is to apply Pythagoras theorem: find the two vertices which are the farthest apart. The remaining vertex is at the right angle (Another way could be calculating the dot product between each pair of edges, and the one which closest to zero forms right angle).
Let's call the vertex at the right angle A
, and the other two B
and C
. Now, the vectors of the two shorter edges of the right triangle is B-A
and C-A
. If you add these edges to A
, you'll get the 4th vertex:
D=A+(B-A)-(C-A)=B+C-A
Ok, i need some slow expanations if it's not a problem. I input 3 vertices by the x and y values in no specific order. Then I'll have to find the right angle point by working on those coordinates. If I find that right angle point it's easy afterwards, just use the formula. But I can't figure out how to code this part with finding this point. Using pythagorian theorem I'd find vertices farthest apart, ok, but I don't actually know how to use that teorem when all I've got are just the coordinates of those vertices. I might be stupid but I'm trying to understand this.
– Krystian Niżeniec
Nov 20 '18 at 0:24
1
@KrystianNiżeniec: you can calculate distance between two points withsqrt((ax-bx)*(ax-bx) + (ay-by)*(ay-by))
. It's basically the simple math formula: distance=sqrt(x^2+y^2), where x=ax-bx and y=ay-by.
– geza
Nov 20 '18 at 0:32
Did it now, then compared all 3 distances and used the formula for each case of the longest one.Should work now, still completes 3 tests though. Will make some error checking if the data input is wrong like same 3 points etc. Thanks!
– Krystian Niżeniec
Nov 20 '18 at 0:41
add a comment |
Working with vectors is a very interesting topic. Here are some good explainations about vector
To answer your question:
From 3 givens vertexs A, B and C, you have only three cases of the right angle : at A, at B or at C. If you found the right angle, for example, at B (no matter order of A and C), you have the D coordinate calculated by the formulation : D = A + C - B.
To detect if the right angle is at B: the dot product of two vectors BA and BC is 0, no matter order of A and C.
In C++ way, (not C way) you should add operators to manipulate vectors in your vertex class, here is an example :
#define MY_EPSILON 10E-6
class vertex {
public:
double X, Y;
vertex(double x_, double y_ ) :X(x_), Y( y_){}
vertex():X(0), Y(0){}
vertex operator +( vertex v ){ return vertex( X + v.X, Y + v.Y ); }
vertex operator -( vertex v ){ return vertex( X - v.X, Y - v.Y ); }
double dot( vertex v ){ return X * v.X + Y * v.Y; }
double length() { return sqrt(X * X + Y * Y ); }
vertex normalize( bool &bOk ){
double len = length(); bOk = false;
if( len > MY_EPSILON ){ bOk = true; return vertex( X/len, Y/len ); }
return *this;
}
};
std::ostream & operator << ( std::ostream & s, vertex v ){
s << std::setprecision(6) << "(" << v.X << "," << v.Y << ") ";
return s;
}
Dot product of two vectors:
To verify if the right angle is at the point B, we can use the following function, it will compute the dot product of two normalized vectors of AB and BC :
bool isRighAngle( vertex a, vertex b, vertex c){
bool bOkAB, bOkBC;
vertex uAB = ( b - a ).normalize( bOkAB ), uBC = ( c - b ).normalize( bOkBC );
return bOkAB && bOkBC && fabs(uAB.dot( uBC )) < MY_EPSILON;
}
Note that when we compaire a double value with zero, use always an epsilon, there is no zero absolut for a double. This function also return false if one of normalized vectors cannot be computed (two points are too close to each other).
Compute the last coordinate from the right angle :
This following function return true if the last cordinate D is calculated from the right angle B:
bool getLastCoordinateIfRightAngle( vertex a, vertex b, vertex c, vertex & d ){
if( isRighAngle( a, b, c ) ){
d = (a + c) - b;
return true;
}
return false;
}
Look for the right angle:
So to find the last coordinate D from 3 vertexs A, B and C, you should do the test for three cases of right angle, the test stops when a solution was found :
bool getLastCoordinate( vertex a, vertex b, vertex c, vertex &d ){
if( getLastCoordinateIfRightAngle( a, b, c, d ) //if B is at the right angle
|| getLastCoordinateIfRightAngle( a, c, b, d ) //if C is at the right angle
|| getLastCoordinateIfRightAngle( b, a, c, d ) ) //if A is at the right angle
{
return true;
}
//No right angle found.
return false;
}
Quick test :
We can do a quick test if it works:
int main(int argc, char *argv)
{
vertex A(0.0, 0.0), B(1.0, 0.0), C(0.0, 1.0), D;
if( getLastCoordinate( A, B, C, D ) ){
std::cout << "D coordinate " << D << " found from inputs : " << A << B << C << std::endl;
}else {
std::cout << "D coordinate not found for input: " << A << B << C << std::endl;
}
return 0;
}
EPSILON CHOICE:
It depends on your domain, if you work in a very small object domain that (X, Y) are very small (close to 10E-5 for example), you will have some difficulties in calculations (floating point in GPU is very limited in precision). It's better to transform the working domain to a normal range.
In the example above, EPSILON is set to 10E-6. If the length between two points is smaller to this value, the two points can be considered as an unique point - they stay in the same position).
Epsilon based comparison should be done more carefully. If edges are long, it could fail for a valid right angle. An easy solution is to normalize vector first, then do the dot product.
– geza
Nov 20 '18 at 13:05
@geza: thanks for pointing out the problem, I've edited my answer to make use of the normalized vector in right angle computation.
– tunglt
Nov 20 '18 at 14:32
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383749%2ffinding-4th-vertex-of-a-rectangle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you have three vertices, then you have half of the rectangle: a right triangle. First you need to determine which point is at the right angle. You can do this in different ways. A way is to apply Pythagoras theorem: find the two vertices which are the farthest apart. The remaining vertex is at the right angle (Another way could be calculating the dot product between each pair of edges, and the one which closest to zero forms right angle).
Let's call the vertex at the right angle A
, and the other two B
and C
. Now, the vectors of the two shorter edges of the right triangle is B-A
and C-A
. If you add these edges to A
, you'll get the 4th vertex:
D=A+(B-A)-(C-A)=B+C-A
Ok, i need some slow expanations if it's not a problem. I input 3 vertices by the x and y values in no specific order. Then I'll have to find the right angle point by working on those coordinates. If I find that right angle point it's easy afterwards, just use the formula. But I can't figure out how to code this part with finding this point. Using pythagorian theorem I'd find vertices farthest apart, ok, but I don't actually know how to use that teorem when all I've got are just the coordinates of those vertices. I might be stupid but I'm trying to understand this.
– Krystian Niżeniec
Nov 20 '18 at 0:24
1
@KrystianNiżeniec: you can calculate distance between two points withsqrt((ax-bx)*(ax-bx) + (ay-by)*(ay-by))
. It's basically the simple math formula: distance=sqrt(x^2+y^2), where x=ax-bx and y=ay-by.
– geza
Nov 20 '18 at 0:32
Did it now, then compared all 3 distances and used the formula for each case of the longest one.Should work now, still completes 3 tests though. Will make some error checking if the data input is wrong like same 3 points etc. Thanks!
– Krystian Niżeniec
Nov 20 '18 at 0:41
add a comment |
If you have three vertices, then you have half of the rectangle: a right triangle. First you need to determine which point is at the right angle. You can do this in different ways. A way is to apply Pythagoras theorem: find the two vertices which are the farthest apart. The remaining vertex is at the right angle (Another way could be calculating the dot product between each pair of edges, and the one which closest to zero forms right angle).
Let's call the vertex at the right angle A
, and the other two B
and C
. Now, the vectors of the two shorter edges of the right triangle is B-A
and C-A
. If you add these edges to A
, you'll get the 4th vertex:
D=A+(B-A)-(C-A)=B+C-A
Ok, i need some slow expanations if it's not a problem. I input 3 vertices by the x and y values in no specific order. Then I'll have to find the right angle point by working on those coordinates. If I find that right angle point it's easy afterwards, just use the formula. But I can't figure out how to code this part with finding this point. Using pythagorian theorem I'd find vertices farthest apart, ok, but I don't actually know how to use that teorem when all I've got are just the coordinates of those vertices. I might be stupid but I'm trying to understand this.
– Krystian Niżeniec
Nov 20 '18 at 0:24
1
@KrystianNiżeniec: you can calculate distance between two points withsqrt((ax-bx)*(ax-bx) + (ay-by)*(ay-by))
. It's basically the simple math formula: distance=sqrt(x^2+y^2), where x=ax-bx and y=ay-by.
– geza
Nov 20 '18 at 0:32
Did it now, then compared all 3 distances and used the formula for each case of the longest one.Should work now, still completes 3 tests though. Will make some error checking if the data input is wrong like same 3 points etc. Thanks!
– Krystian Niżeniec
Nov 20 '18 at 0:41
add a comment |
If you have three vertices, then you have half of the rectangle: a right triangle. First you need to determine which point is at the right angle. You can do this in different ways. A way is to apply Pythagoras theorem: find the two vertices which are the farthest apart. The remaining vertex is at the right angle (Another way could be calculating the dot product between each pair of edges, and the one which closest to zero forms right angle).
Let's call the vertex at the right angle A
, and the other two B
and C
. Now, the vectors of the two shorter edges of the right triangle is B-A
and C-A
. If you add these edges to A
, you'll get the 4th vertex:
D=A+(B-A)-(C-A)=B+C-A
If you have three vertices, then you have half of the rectangle: a right triangle. First you need to determine which point is at the right angle. You can do this in different ways. A way is to apply Pythagoras theorem: find the two vertices which are the farthest apart. The remaining vertex is at the right angle (Another way could be calculating the dot product between each pair of edges, and the one which closest to zero forms right angle).
Let's call the vertex at the right angle A
, and the other two B
and C
. Now, the vectors of the two shorter edges of the right triangle is B-A
and C-A
. If you add these edges to A
, you'll get the 4th vertex:
D=A+(B-A)-(C-A)=B+C-A
answered Nov 20 '18 at 0:08
gezageza
12.7k32775
12.7k32775
Ok, i need some slow expanations if it's not a problem. I input 3 vertices by the x and y values in no specific order. Then I'll have to find the right angle point by working on those coordinates. If I find that right angle point it's easy afterwards, just use the formula. But I can't figure out how to code this part with finding this point. Using pythagorian theorem I'd find vertices farthest apart, ok, but I don't actually know how to use that teorem when all I've got are just the coordinates of those vertices. I might be stupid but I'm trying to understand this.
– Krystian Niżeniec
Nov 20 '18 at 0:24
1
@KrystianNiżeniec: you can calculate distance between two points withsqrt((ax-bx)*(ax-bx) + (ay-by)*(ay-by))
. It's basically the simple math formula: distance=sqrt(x^2+y^2), where x=ax-bx and y=ay-by.
– geza
Nov 20 '18 at 0:32
Did it now, then compared all 3 distances and used the formula for each case of the longest one.Should work now, still completes 3 tests though. Will make some error checking if the data input is wrong like same 3 points etc. Thanks!
– Krystian Niżeniec
Nov 20 '18 at 0:41
add a comment |
Ok, i need some slow expanations if it's not a problem. I input 3 vertices by the x and y values in no specific order. Then I'll have to find the right angle point by working on those coordinates. If I find that right angle point it's easy afterwards, just use the formula. But I can't figure out how to code this part with finding this point. Using pythagorian theorem I'd find vertices farthest apart, ok, but I don't actually know how to use that teorem when all I've got are just the coordinates of those vertices. I might be stupid but I'm trying to understand this.
– Krystian Niżeniec
Nov 20 '18 at 0:24
1
@KrystianNiżeniec: you can calculate distance between two points withsqrt((ax-bx)*(ax-bx) + (ay-by)*(ay-by))
. It's basically the simple math formula: distance=sqrt(x^2+y^2), where x=ax-bx and y=ay-by.
– geza
Nov 20 '18 at 0:32
Did it now, then compared all 3 distances and used the formula for each case of the longest one.Should work now, still completes 3 tests though. Will make some error checking if the data input is wrong like same 3 points etc. Thanks!
– Krystian Niżeniec
Nov 20 '18 at 0:41
Ok, i need some slow expanations if it's not a problem. I input 3 vertices by the x and y values in no specific order. Then I'll have to find the right angle point by working on those coordinates. If I find that right angle point it's easy afterwards, just use the formula. But I can't figure out how to code this part with finding this point. Using pythagorian theorem I'd find vertices farthest apart, ok, but I don't actually know how to use that teorem when all I've got are just the coordinates of those vertices. I might be stupid but I'm trying to understand this.
– Krystian Niżeniec
Nov 20 '18 at 0:24
Ok, i need some slow expanations if it's not a problem. I input 3 vertices by the x and y values in no specific order. Then I'll have to find the right angle point by working on those coordinates. If I find that right angle point it's easy afterwards, just use the formula. But I can't figure out how to code this part with finding this point. Using pythagorian theorem I'd find vertices farthest apart, ok, but I don't actually know how to use that teorem when all I've got are just the coordinates of those vertices. I might be stupid but I'm trying to understand this.
– Krystian Niżeniec
Nov 20 '18 at 0:24
1
1
@KrystianNiżeniec: you can calculate distance between two points with
sqrt((ax-bx)*(ax-bx) + (ay-by)*(ay-by))
. It's basically the simple math formula: distance=sqrt(x^2+y^2), where x=ax-bx and y=ay-by.– geza
Nov 20 '18 at 0:32
@KrystianNiżeniec: you can calculate distance between two points with
sqrt((ax-bx)*(ax-bx) + (ay-by)*(ay-by))
. It's basically the simple math formula: distance=sqrt(x^2+y^2), where x=ax-bx and y=ay-by.– geza
Nov 20 '18 at 0:32
Did it now, then compared all 3 distances and used the formula for each case of the longest one.Should work now, still completes 3 tests though. Will make some error checking if the data input is wrong like same 3 points etc. Thanks!
– Krystian Niżeniec
Nov 20 '18 at 0:41
Did it now, then compared all 3 distances and used the formula for each case of the longest one.Should work now, still completes 3 tests though. Will make some error checking if the data input is wrong like same 3 points etc. Thanks!
– Krystian Niżeniec
Nov 20 '18 at 0:41
add a comment |
Working with vectors is a very interesting topic. Here are some good explainations about vector
To answer your question:
From 3 givens vertexs A, B and C, you have only three cases of the right angle : at A, at B or at C. If you found the right angle, for example, at B (no matter order of A and C), you have the D coordinate calculated by the formulation : D = A + C - B.
To detect if the right angle is at B: the dot product of two vectors BA and BC is 0, no matter order of A and C.
In C++ way, (not C way) you should add operators to manipulate vectors in your vertex class, here is an example :
#define MY_EPSILON 10E-6
class vertex {
public:
double X, Y;
vertex(double x_, double y_ ) :X(x_), Y( y_){}
vertex():X(0), Y(0){}
vertex operator +( vertex v ){ return vertex( X + v.X, Y + v.Y ); }
vertex operator -( vertex v ){ return vertex( X - v.X, Y - v.Y ); }
double dot( vertex v ){ return X * v.X + Y * v.Y; }
double length() { return sqrt(X * X + Y * Y ); }
vertex normalize( bool &bOk ){
double len = length(); bOk = false;
if( len > MY_EPSILON ){ bOk = true; return vertex( X/len, Y/len ); }
return *this;
}
};
std::ostream & operator << ( std::ostream & s, vertex v ){
s << std::setprecision(6) << "(" << v.X << "," << v.Y << ") ";
return s;
}
Dot product of two vectors:
To verify if the right angle is at the point B, we can use the following function, it will compute the dot product of two normalized vectors of AB and BC :
bool isRighAngle( vertex a, vertex b, vertex c){
bool bOkAB, bOkBC;
vertex uAB = ( b - a ).normalize( bOkAB ), uBC = ( c - b ).normalize( bOkBC );
return bOkAB && bOkBC && fabs(uAB.dot( uBC )) < MY_EPSILON;
}
Note that when we compaire a double value with zero, use always an epsilon, there is no zero absolut for a double. This function also return false if one of normalized vectors cannot be computed (two points are too close to each other).
Compute the last coordinate from the right angle :
This following function return true if the last cordinate D is calculated from the right angle B:
bool getLastCoordinateIfRightAngle( vertex a, vertex b, vertex c, vertex & d ){
if( isRighAngle( a, b, c ) ){
d = (a + c) - b;
return true;
}
return false;
}
Look for the right angle:
So to find the last coordinate D from 3 vertexs A, B and C, you should do the test for three cases of right angle, the test stops when a solution was found :
bool getLastCoordinate( vertex a, vertex b, vertex c, vertex &d ){
if( getLastCoordinateIfRightAngle( a, b, c, d ) //if B is at the right angle
|| getLastCoordinateIfRightAngle( a, c, b, d ) //if C is at the right angle
|| getLastCoordinateIfRightAngle( b, a, c, d ) ) //if A is at the right angle
{
return true;
}
//No right angle found.
return false;
}
Quick test :
We can do a quick test if it works:
int main(int argc, char *argv)
{
vertex A(0.0, 0.0), B(1.0, 0.0), C(0.0, 1.0), D;
if( getLastCoordinate( A, B, C, D ) ){
std::cout << "D coordinate " << D << " found from inputs : " << A << B << C << std::endl;
}else {
std::cout << "D coordinate not found for input: " << A << B << C << std::endl;
}
return 0;
}
EPSILON CHOICE:
It depends on your domain, if you work in a very small object domain that (X, Y) are very small (close to 10E-5 for example), you will have some difficulties in calculations (floating point in GPU is very limited in precision). It's better to transform the working domain to a normal range.
In the example above, EPSILON is set to 10E-6. If the length between two points is smaller to this value, the two points can be considered as an unique point - they stay in the same position).
Epsilon based comparison should be done more carefully. If edges are long, it could fail for a valid right angle. An easy solution is to normalize vector first, then do the dot product.
– geza
Nov 20 '18 at 13:05
@geza: thanks for pointing out the problem, I've edited my answer to make use of the normalized vector in right angle computation.
– tunglt
Nov 20 '18 at 14:32
add a comment |
Working with vectors is a very interesting topic. Here are some good explainations about vector
To answer your question:
From 3 givens vertexs A, B and C, you have only three cases of the right angle : at A, at B or at C. If you found the right angle, for example, at B (no matter order of A and C), you have the D coordinate calculated by the formulation : D = A + C - B.
To detect if the right angle is at B: the dot product of two vectors BA and BC is 0, no matter order of A and C.
In C++ way, (not C way) you should add operators to manipulate vectors in your vertex class, here is an example :
#define MY_EPSILON 10E-6
class vertex {
public:
double X, Y;
vertex(double x_, double y_ ) :X(x_), Y( y_){}
vertex():X(0), Y(0){}
vertex operator +( vertex v ){ return vertex( X + v.X, Y + v.Y ); }
vertex operator -( vertex v ){ return vertex( X - v.X, Y - v.Y ); }
double dot( vertex v ){ return X * v.X + Y * v.Y; }
double length() { return sqrt(X * X + Y * Y ); }
vertex normalize( bool &bOk ){
double len = length(); bOk = false;
if( len > MY_EPSILON ){ bOk = true; return vertex( X/len, Y/len ); }
return *this;
}
};
std::ostream & operator << ( std::ostream & s, vertex v ){
s << std::setprecision(6) << "(" << v.X << "," << v.Y << ") ";
return s;
}
Dot product of two vectors:
To verify if the right angle is at the point B, we can use the following function, it will compute the dot product of two normalized vectors of AB and BC :
bool isRighAngle( vertex a, vertex b, vertex c){
bool bOkAB, bOkBC;
vertex uAB = ( b - a ).normalize( bOkAB ), uBC = ( c - b ).normalize( bOkBC );
return bOkAB && bOkBC && fabs(uAB.dot( uBC )) < MY_EPSILON;
}
Note that when we compaire a double value with zero, use always an epsilon, there is no zero absolut for a double. This function also return false if one of normalized vectors cannot be computed (two points are too close to each other).
Compute the last coordinate from the right angle :
This following function return true if the last cordinate D is calculated from the right angle B:
bool getLastCoordinateIfRightAngle( vertex a, vertex b, vertex c, vertex & d ){
if( isRighAngle( a, b, c ) ){
d = (a + c) - b;
return true;
}
return false;
}
Look for the right angle:
So to find the last coordinate D from 3 vertexs A, B and C, you should do the test for three cases of right angle, the test stops when a solution was found :
bool getLastCoordinate( vertex a, vertex b, vertex c, vertex &d ){
if( getLastCoordinateIfRightAngle( a, b, c, d ) //if B is at the right angle
|| getLastCoordinateIfRightAngle( a, c, b, d ) //if C is at the right angle
|| getLastCoordinateIfRightAngle( b, a, c, d ) ) //if A is at the right angle
{
return true;
}
//No right angle found.
return false;
}
Quick test :
We can do a quick test if it works:
int main(int argc, char *argv)
{
vertex A(0.0, 0.0), B(1.0, 0.0), C(0.0, 1.0), D;
if( getLastCoordinate( A, B, C, D ) ){
std::cout << "D coordinate " << D << " found from inputs : " << A << B << C << std::endl;
}else {
std::cout << "D coordinate not found for input: " << A << B << C << std::endl;
}
return 0;
}
EPSILON CHOICE:
It depends on your domain, if you work in a very small object domain that (X, Y) are very small (close to 10E-5 for example), you will have some difficulties in calculations (floating point in GPU is very limited in precision). It's better to transform the working domain to a normal range.
In the example above, EPSILON is set to 10E-6. If the length between two points is smaller to this value, the two points can be considered as an unique point - they stay in the same position).
Epsilon based comparison should be done more carefully. If edges are long, it could fail for a valid right angle. An easy solution is to normalize vector first, then do the dot product.
– geza
Nov 20 '18 at 13:05
@geza: thanks for pointing out the problem, I've edited my answer to make use of the normalized vector in right angle computation.
– tunglt
Nov 20 '18 at 14:32
add a comment |
Working with vectors is a very interesting topic. Here are some good explainations about vector
To answer your question:
From 3 givens vertexs A, B and C, you have only three cases of the right angle : at A, at B or at C. If you found the right angle, for example, at B (no matter order of A and C), you have the D coordinate calculated by the formulation : D = A + C - B.
To detect if the right angle is at B: the dot product of two vectors BA and BC is 0, no matter order of A and C.
In C++ way, (not C way) you should add operators to manipulate vectors in your vertex class, here is an example :
#define MY_EPSILON 10E-6
class vertex {
public:
double X, Y;
vertex(double x_, double y_ ) :X(x_), Y( y_){}
vertex():X(0), Y(0){}
vertex operator +( vertex v ){ return vertex( X + v.X, Y + v.Y ); }
vertex operator -( vertex v ){ return vertex( X - v.X, Y - v.Y ); }
double dot( vertex v ){ return X * v.X + Y * v.Y; }
double length() { return sqrt(X * X + Y * Y ); }
vertex normalize( bool &bOk ){
double len = length(); bOk = false;
if( len > MY_EPSILON ){ bOk = true; return vertex( X/len, Y/len ); }
return *this;
}
};
std::ostream & operator << ( std::ostream & s, vertex v ){
s << std::setprecision(6) << "(" << v.X << "," << v.Y << ") ";
return s;
}
Dot product of two vectors:
To verify if the right angle is at the point B, we can use the following function, it will compute the dot product of two normalized vectors of AB and BC :
bool isRighAngle( vertex a, vertex b, vertex c){
bool bOkAB, bOkBC;
vertex uAB = ( b - a ).normalize( bOkAB ), uBC = ( c - b ).normalize( bOkBC );
return bOkAB && bOkBC && fabs(uAB.dot( uBC )) < MY_EPSILON;
}
Note that when we compaire a double value with zero, use always an epsilon, there is no zero absolut for a double. This function also return false if one of normalized vectors cannot be computed (two points are too close to each other).
Compute the last coordinate from the right angle :
This following function return true if the last cordinate D is calculated from the right angle B:
bool getLastCoordinateIfRightAngle( vertex a, vertex b, vertex c, vertex & d ){
if( isRighAngle( a, b, c ) ){
d = (a + c) - b;
return true;
}
return false;
}
Look for the right angle:
So to find the last coordinate D from 3 vertexs A, B and C, you should do the test for three cases of right angle, the test stops when a solution was found :
bool getLastCoordinate( vertex a, vertex b, vertex c, vertex &d ){
if( getLastCoordinateIfRightAngle( a, b, c, d ) //if B is at the right angle
|| getLastCoordinateIfRightAngle( a, c, b, d ) //if C is at the right angle
|| getLastCoordinateIfRightAngle( b, a, c, d ) ) //if A is at the right angle
{
return true;
}
//No right angle found.
return false;
}
Quick test :
We can do a quick test if it works:
int main(int argc, char *argv)
{
vertex A(0.0, 0.0), B(1.0, 0.0), C(0.0, 1.0), D;
if( getLastCoordinate( A, B, C, D ) ){
std::cout << "D coordinate " << D << " found from inputs : " << A << B << C << std::endl;
}else {
std::cout << "D coordinate not found for input: " << A << B << C << std::endl;
}
return 0;
}
EPSILON CHOICE:
It depends on your domain, if you work in a very small object domain that (X, Y) are very small (close to 10E-5 for example), you will have some difficulties in calculations (floating point in GPU is very limited in precision). It's better to transform the working domain to a normal range.
In the example above, EPSILON is set to 10E-6. If the length between two points is smaller to this value, the two points can be considered as an unique point - they stay in the same position).
Working with vectors is a very interesting topic. Here are some good explainations about vector
To answer your question:
From 3 givens vertexs A, B and C, you have only three cases of the right angle : at A, at B or at C. If you found the right angle, for example, at B (no matter order of A and C), you have the D coordinate calculated by the formulation : D = A + C - B.
To detect if the right angle is at B: the dot product of two vectors BA and BC is 0, no matter order of A and C.
In C++ way, (not C way) you should add operators to manipulate vectors in your vertex class, here is an example :
#define MY_EPSILON 10E-6
class vertex {
public:
double X, Y;
vertex(double x_, double y_ ) :X(x_), Y( y_){}
vertex():X(0), Y(0){}
vertex operator +( vertex v ){ return vertex( X + v.X, Y + v.Y ); }
vertex operator -( vertex v ){ return vertex( X - v.X, Y - v.Y ); }
double dot( vertex v ){ return X * v.X + Y * v.Y; }
double length() { return sqrt(X * X + Y * Y ); }
vertex normalize( bool &bOk ){
double len = length(); bOk = false;
if( len > MY_EPSILON ){ bOk = true; return vertex( X/len, Y/len ); }
return *this;
}
};
std::ostream & operator << ( std::ostream & s, vertex v ){
s << std::setprecision(6) << "(" << v.X << "," << v.Y << ") ";
return s;
}
Dot product of two vectors:
To verify if the right angle is at the point B, we can use the following function, it will compute the dot product of two normalized vectors of AB and BC :
bool isRighAngle( vertex a, vertex b, vertex c){
bool bOkAB, bOkBC;
vertex uAB = ( b - a ).normalize( bOkAB ), uBC = ( c - b ).normalize( bOkBC );
return bOkAB && bOkBC && fabs(uAB.dot( uBC )) < MY_EPSILON;
}
Note that when we compaire a double value with zero, use always an epsilon, there is no zero absolut for a double. This function also return false if one of normalized vectors cannot be computed (two points are too close to each other).
Compute the last coordinate from the right angle :
This following function return true if the last cordinate D is calculated from the right angle B:
bool getLastCoordinateIfRightAngle( vertex a, vertex b, vertex c, vertex & d ){
if( isRighAngle( a, b, c ) ){
d = (a + c) - b;
return true;
}
return false;
}
Look for the right angle:
So to find the last coordinate D from 3 vertexs A, B and C, you should do the test for three cases of right angle, the test stops when a solution was found :
bool getLastCoordinate( vertex a, vertex b, vertex c, vertex &d ){
if( getLastCoordinateIfRightAngle( a, b, c, d ) //if B is at the right angle
|| getLastCoordinateIfRightAngle( a, c, b, d ) //if C is at the right angle
|| getLastCoordinateIfRightAngle( b, a, c, d ) ) //if A is at the right angle
{
return true;
}
//No right angle found.
return false;
}
Quick test :
We can do a quick test if it works:
int main(int argc, char *argv)
{
vertex A(0.0, 0.0), B(1.0, 0.0), C(0.0, 1.0), D;
if( getLastCoordinate( A, B, C, D ) ){
std::cout << "D coordinate " << D << " found from inputs : " << A << B << C << std::endl;
}else {
std::cout << "D coordinate not found for input: " << A << B << C << std::endl;
}
return 0;
}
EPSILON CHOICE:
It depends on your domain, if you work in a very small object domain that (X, Y) are very small (close to 10E-5 for example), you will have some difficulties in calculations (floating point in GPU is very limited in precision). It's better to transform the working domain to a normal range.
In the example above, EPSILON is set to 10E-6. If the length between two points is smaller to this value, the two points can be considered as an unique point - they stay in the same position).
edited Nov 20 '18 at 15:19
answered Nov 20 '18 at 11:59
tunglttunglt
59928
59928
Epsilon based comparison should be done more carefully. If edges are long, it could fail for a valid right angle. An easy solution is to normalize vector first, then do the dot product.
– geza
Nov 20 '18 at 13:05
@geza: thanks for pointing out the problem, I've edited my answer to make use of the normalized vector in right angle computation.
– tunglt
Nov 20 '18 at 14:32
add a comment |
Epsilon based comparison should be done more carefully. If edges are long, it could fail for a valid right angle. An easy solution is to normalize vector first, then do the dot product.
– geza
Nov 20 '18 at 13:05
@geza: thanks for pointing out the problem, I've edited my answer to make use of the normalized vector in right angle computation.
– tunglt
Nov 20 '18 at 14:32
Epsilon based comparison should be done more carefully. If edges are long, it could fail for a valid right angle. An easy solution is to normalize vector first, then do the dot product.
– geza
Nov 20 '18 at 13:05
Epsilon based comparison should be done more carefully. If edges are long, it could fail for a valid right angle. An easy solution is to normalize vector first, then do the dot product.
– geza
Nov 20 '18 at 13:05
@geza: thanks for pointing out the problem, I've edited my answer to make use of the normalized vector in right angle computation.
– tunglt
Nov 20 '18 at 14:32
@geza: thanks for pointing out the problem, I've edited my answer to make use of the normalized vector in right angle computation.
– tunglt
Nov 20 '18 at 14:32
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383749%2ffinding-4th-vertex-of-a-rectangle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Lets say I give you (0,0), (1,0), (0, -1) as A, B and C. Would calculate give you (1, -1)? Can you see the math that would?
– NathanOliver
Nov 19 '18 at 22:50
Always pass objects by reference, not by value!
– SilvioCro
Nov 19 '18 at 22:57
@NathanOliver It acually does if I input than in that specific order, but doesn't if I input it for example backwards. What could solve that problem?
– Krystian Niżeniec
Nov 19 '18 at 23:02
@SilvioCro Changed it, thanks! Our teacher didn't say that it's a must.
– Krystian Niżeniec
Nov 19 '18 at 23:04
1
You should use a dot product to check if A, B, C are correct rectangle coordinates. Then in vector form : DC = AB or D = C - (B - A).
– tunglt
Nov 19 '18 at 23:19