Find angle of right angled triangle - geometry

I am trying to work out the angle of a right angled triangle. I have an array containing the lengths of the two sides of the triangle. I also have an array containing the Euclidean Distance between these two points. How would I find the angle of the triangles? In other words, how would I do the sin and then arcsin methods to find the angle? I am just looking for the angle opposite of the hypotenuse. I'm trying to do this in C++.
Solved it now, misinterpreted what I had been asked to do

Solution: How would I find the angle of the triangles
#include <iostream>
#include <cmath>
using namespace std;
#define radians(x) return x * (180/pi)
int main()
{
double opposite, adjacent, angle1, angle2, angle3, choice, radians, hypotenuse;
cout << "Opposite: ";
cin >> opposite;
cout << "Adjacent: ";
cin >> adjacent;
cout << "Radians or Degrees: (R/D)";
cin >> choice;
if(choice == "R")
{
angle1 = arctan(adjacent/opposite);
hypotenuse = opposite\cos(radians(angle1));
angle2 = arcsin(adjacent\hypotenuse);
cout << "Angle 1: "<< radians(angle1) << endl;
cout << "Angle 2: "<< "90\n";
cout << "Angle 3: "<< radians(angle2) << endl;
cout << "Hypotenuse: " << hypotenuse;
}
else if(choice = "D")
{
angle1 = arctan(adjacent/opposite);
hypotenuse = opposite\cos((angle1));
angle2 = arcsin(adjacent\hypotenuse);
cout << "Angle 1: " << (angle1) << endl;
cout << "Angle 2: " << "90\n";
cout << "Angle 3: " << (angle2) << endl;
cout << "Hypotenuse: " << hypotenuse;
}
return 0;
}
or just
angle2 = 180 - (angle1 + 90)

The relation between sides and angles of triangle is:-
a/sinA = b/sinB = c/sinC
where 'a' is the side opposite angle 'A'.
You know one angle let's say it's A = 90. Then you can calculate other two angles from above equation.

You have the lengths of the sides, if you us tangents, you can find the angle for the corresponding side.
Also, once you find one angle, all you need to do is subtract 90 from it to get the final angle:
tan(angle) = opposite/adjacent;
angle = arctan(opposite/adjacent);
otherAngle = 90 - angle;

Related

in c++ how do I calculate tool path length?

I am dealing with generation of tool path where composed many points in three dimension and I am using CNC machine to generate them. One of the things that I want to calculate is tool path length which is defined the total length of path. So I tried this:
1.6760 3.7901 6.1955
1.2788 4.1872 5.3681
0.2832 5.1828 3.2939
0.1835 5.2173 3.0576
0.1097 5.1205 2.8292
0.0815 4.9185 2.6699
0.0812 4.8728 2.6491
0.0810 4.8270 2.6288
0.0807 4.7810 2.6089
The points are like these.
// math.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
#include<math.h>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::ostream;
using std::istream;
using std::ifstream;
using std::operator>>;
using std::operator<<;
struct point
{
float x ;
float y ;
float z ;
};
ostream& operator<< (ostream& out, const point &p)
{
out << "(" << p.x << "," << p.y << " ," << p.z << "," << ")";
return out;
}
istream& operator>> (istream& in, point& point)
{
in >> point.x >> point.y >> point.z;
return in;
}
struct line
{
point start;
point next;
float sqDistance()
{
float dx = start.x - next.x;
float dy = start.y - next.y;
float dz = start.z - next.z;
double distance = 0.0;
distance = sqrt(dx * dx + dy * dy + dz * dz);
return distance;
}
};
ostream& operator<< (ostream& out, const line &ln)
{
out << "From " << ln.start << " to " << ln.next;
return out;
}
istream& operator>> (istream& in, line ln)
{
cout << "Enter x y z start then x y z next: ";
in >> ln.start.x >> ln.start.y >> ln.start.z >> ln.next.x >> ln.next.y >> ln.next.z;
return in;
}
int main()
{
point origin, input;
line ray;
vector<line> side;
// READ POINTS FROM FILE
ifstream pointfile("concave.txt");
if (pointfile.is_open())
{
pointfile >> origin.x >> origin.y >> origin.z;
cout << "origin: " << origin << endl;
ray.start = origin;
while (pointfile >> ray.next)
{
cout
<< " GOTO/ " << ray.next
<< " The distance from point to the next is : "
<< ray.sqDistance() << endl;
side.push_back(ray);
}
}
else
cout << "Unable to open file";
pointfile.close();
vector<line>::iterator iter = side.begin();
line temp, closest = *iter;
float minimumDistance = closest.sqDistance(), distance = 0.0;
system("PAUSE");
return 0;
}
-I expect the distance between point and its next point.
-the total length of this line.
What about something like this - suppose you want distance between points and not from start (line 93 with comment):
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <vector>
#include<math.h>
#include <conio.h>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::ostream;
using std::istream;
using std::ifstream;
using std::operator>>;
using std::operator<<;
struct point
{
float x ;
float y ;
float z ;
};
ostream& operator<< (ostream& out, const point &p)
{
out << "(" << p.x << "," << p.y << " ," << p.z << "," << ")";
return out;
}
istream& operator>> (istream& in, point& point)
{
in >> point.x >> point.y >> point.z;
return in;
}
struct line
{
point start;
point next;
float sqDistance()
{
float dx = start.x - next.x;
float dy = start.y - next.y;
float dz = start.z - next.z;
double distance = 0.0;
distance = sqrt(dx * dx + dy * dy + dz * dz);
return distance;
}
};
ostream& operator<< (ostream& out, const line &ln)
{
out << "From " << ln.start << " to " << ln.next;
return out;
}
istream& operator>> (istream& in, line ln)
{
cout << "Enter x y z start then x y z next: ";
in >> ln.start.x >> ln.start.y >> ln.start.z >> ln.next.x >> ln.next.y >> ln.next.z;
return in;
}
int main()
{
point origin, input;
line ray;
vector<line> side;
// READ POINTS FROM FILE
ifstream pointfile("concave.txt");
if (pointfile.is_open())
{
pointfile >> origin.x >> origin.y >> origin.z;
cout << "origin: " << origin << endl;
ray.start = origin;
while (pointfile >> ray.next)
{
cout
<< " GOTO/ " << ray.next
<< " The distance from point to the next is : "
<< ray.sqDistance() << endl;
side.push_back(ray);
ray.start = ray.next; // set start to last end (?)
}
}
else
cout << "Unable to open file";
pointfile.close();
vector<line>::iterator iter = side.begin();
line temp, closest = *iter;
float minimumDistance = closest.sqDistance(), distance, sumDistance = 0.0;
cout << "Line coords" << endl << "distance, Sum of distances, minimum" << endl;
while(iter != side.end()) {
closest = *iter;
distance = closest.sqDistance();
sumDistance += distance;
if(minimumDistance > distance) minimumDistance = distance;
cout << closest << endl
<< distance << " | " << sumDistance << " | " << minimumDistance << endl;
sumDistance += distance;
iter++;
}
getch();
return 0;
}
Sorry for the bug - sum line was there twice - line after cout was an error, also noticed some precision warning - mixed double/float, so switched to double everywhere, now main loop looks like this:
vector<line>::iterator iter = side.begin();
line closest = *iter;
double distance, sumOfDistances = 0.0;
cout << "Line coords" << endl << "distance | Sum of distances" << endl;
while (iter != side.end()) {
closest = *iter;
distance = closest.sqDistance();
sumOfDistances += distance;
cout << closest << endl << distance << " | " << sumOfDistances << endl; // step info output
iter++;
}
Here complete, a bit shorter version including simple results.txt file output.
You can remove 2 lines with comment info output:
#include <iostream>
#include <fstream>
#include <vector>
#include <conio.h>
using namespace std;
struct point
{
float x;
float y;
float z;
};
ostream& operator<< (ostream& out, const point &p)
{
out << "(" << p.x << "," << p.y << " ," << p.z << "," << ")";
return out;
}
istream& operator>> (istream& in, point& point)
{
in >> point.x >> point.y >> point.z;
return in;
}
struct line
{
point start;
point next;
double sqDistance()
{
float dx = start.x - next.x;
float dy = start.y - next.y;
float dz = start.z - next.z;
double distance = 0.0;
distance = sqrt(dx * dx + dy * dy + dz * dz);
return distance;
}
};
ostream& operator<< (ostream& out, const line &ln)
{
out << "From " << ln.start << " to " << ln.next;
return out;
}
istream& operator>> (istream& in, line ln)
{
cout << "Enter x y z start then x y z next: ";
in >> ln.start.x >> ln.start.y >> ln.start.z >> ln.next.x >> ln.next.y >> ln.next.z;
return in;
}
int main()
{
point origin, input;
line ray;
vector<line> side;
// READ POINTS FROM FILE
ifstream pointfile("concave.txt");
if (pointfile.is_open())
{
pointfile >> origin.x >> origin.y >> origin.z;
cout << "origin: " << origin << endl;
ray.start = origin;
while (pointfile >> ray.next)
{
cout
<< " GOTO/ " << ray.next
<< " The distance from point to the next is : "
<< ray.sqDistance() << endl;
side.push_back(ray);
ray.start = ray.next; // set start to last end (?)
}
}
else
cout << "Unable to open file";
pointfile.close();
ofstream results("results.txt");
vector<line>::iterator iter = side.begin();
line closest = *iter;
double distance, sumOfDistances = 0.0;
cout << "Line coords" << endl << "distance | Sum of distances" << endl;
while (iter != side.end()) {
closest = *iter;
distance = closest.sqDistance();
sumOfDistances += distance;
results << distance << endl;
cout << closest << endl << distance << " | " << sumOfDistances << endl; // info output
iter++;
}
results << sumOfDistances << " << Sum" << endl;
results.close();
cout << "Complete path distance: " << sumOfDistances << endl; // info output
getch();
return 0;
}

I'm trying to make a loop to draw multiple lines in cairo but it stops drawing after the first iteration

I'm making a program where a person can input a direction and in the if statement, it adds/subtracts x/y axis and it draws a line after it gets over. The problem is that for some reason, it only works at the first iteration and doesn't draw any more lines after that.
I added a cin >> x >> y to test it out but it only draws one line and doesn't draw anymore.
Initially, the choices were in a switch statement but I changed to if because I thought that was causing the error.
#include "pch.h"
#include <iostream>
#include <fstream>
#include <string.h>
#include <cairo.h>
#define _USE_MATH_DEFINES
#include <math.h>
using namespace std;
char b = NULL;
char u = 'œ';
char d = 'd';
int main()
{
cairo_surface_t *surface = cairo_image_surface_create_from_png("background.png");
cairo_t *cr = cairo_create(surface);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_set_line_width(cr, 5);
double x = 0, y = 240;
cairo_move_to(cr, x, y);
long int count = 0;
int cl = 0;
int crr = 0;
int choice = 0;
int n;
system("cls");
while (choice != 5)
{
cin >> x >> y;
cairo_line_to(cr, x, y);
cairo_stroke(cr);
cairo_surface_write_to_png(surface, "spiral.png");
cout << "Current no. of points are : " << count << "/4096" << endl;
cout << "Enter direction: \n" << endl;
cout << "1 - Top Left \t 2 - Up \t 3 - Top Right " << endl;
cout << "4 - Left \t 5 - Stop \t 6 - Right" << endl;
cout << "7 - Bot. Left \t 8 - Down \t 9 - Bot. Right" << endl << endl;
cout << "Enter you choice: ";
cin >> choice;
if (choice == 1)
cout << "Test";
else
{
//More choices include the direction the person needs to go and it subtracts/adds to the x/y part
cout << "How many times ?: ";
cin >> n;
for (int i = 1; i <= n; i++)
{
x++;
count++;
cl++;
if (cl == 256)
{
cl = 0;
crr++;
}
}
system("cls");
}
}
}
I expect it to draw lines to a particular direction. Say the person inputs right, it draws a line towards right and so on. But here, no lines get drawn at all (except if I add a cin >> x >> y at the start of the while loop, that draws one line and that's it, no more lines.)
This fails because there is no current point anymore. After cairo_stroke(cr);, you can add cairo_move_to(cr, x, y); and it should start drawing more lines in the way you expect. I think... I'm not quite sure what you are up to with this program.

Shape Transformers and Interfaces OpenCV3.0

I was trying to make use of the new Shape Transformers and Interfaces of OpenCV3.0. Unfortunately it doesn't work as expected. To ensure not making any fancy warps and getting strange results cause of that reason I initialized a transformation where nothing at all should happen. But output of the transformation for a testpoint is always [0,0] and the warped image is always completley gray. Any suggestions what could be wrong are welcome.
int main(void){
Mat img1 = imread("C:\\opencv\\sources\\samples\\data\\graf1.png", IMREAD_GRAYSCALE);
std::vector<cv::Point2f> points1, testpoints;
vector<DMatch> good_matches;
Mat respic, resmat;
points1.push_back(Point(0, 0)); //Corners 800x600 pic
points1.push_back(Point(799, 0));
points1.push_back(Point(799, 599));
points1.push_back(Point(0, 599));
Mat pointmatrix1(points1);
good_matches.push_back(DMatch(0, 0, 0));
good_matches.push_back(DMatch(1, 1, 0));
good_matches.push_back(DMatch(2, 2, 0));
good_matches.push_back(DMatch(3, 3, 0));
testpoints.push_back(Point(250, 250));
Mat testpointsmat(testpoints);
// Apply TPS
Ptr<ThinPlateSplineShapeTransformer> mytps = createThinPlateSplineShapeTransformer(0);
mytps->estimateTransformation(pointmatrix1, pointmatrix1, good_matches); // Using same pointmatrix nothing should change in res
mytps->applyTransformation(testpointsmat, resmat);
cout << "pointmatrix1 = " << endl << " " << pointmatrix1 << endl << endl;
cout << "testpointsmat = " << endl << " " << testpointsmat << endl << endl;
cout << "resmat = " << endl << " " << resmat << endl << endl; //Always [0,0] ?
imshow("img1", img1); // Just to see if I have a good picture
mytps->warpImage(img1, respic);
imwrite("Tranformed.png", respic);
imshow("Tranformed", respic); //Always completley grey ?
waitKey(0);
return 0;
}
Don't ask me why but if I add this two lines it works.
// Apply TPS
transpose(pointmatrix1, pointmatrix1); // ADD
transpose(testpoints, testpoints); // ADD
Ptr<ThinPlateSplineShapeTransformer> mytps = createThinPlateSplineShapeTransformer(0);
Now There is something strange in source code here why cols and not rows.
by LBerger

How do I get only one answer to display in simple C++ calculator?

I am a novice programmer here so please be kind:
I am writing a C++ program that performs simple arithmetic. I have everything syntactically correct, but multiple answers are showing up e.g. each of my separate cout statements after after the answer is computer show up when + is used, but the subsequent cout statements as other operators are used (-, *, /) show only a few of them. I could use the help here is the code.
//This program will take two integers and compute them in basic arithmetic
//in the way that a simple calculator would.
#include <iostream>
using namespace std;
int main ()
{
int num1;
int num2;
double sum, difference, product, quotient;
char operSymbol;
cout << "Please enter the first number you would like to equate: ";
cin >> num1;
cout << "Please enter the second number: ";
cin >> num2;
cout << "Please choose the operator you would like to use (+, -, *, /): ";
cin >> operSymbol;
switch (operSymbol)
{
case '+':
sum = num1 + num2;
cout << "The sum is: " << sum << endl;
case '-':
difference = num1 - num2;
cout << "The difference is: " << difference << endl;
case '*':
product = num1 * num2;
cout << "The product is: " << product << endl;
case '/':
quotient = num1 / num2;
cout << "The quotient is: " << quotient << endl;
}
system("Pause");
return 0;
}
You need to explicitly end the execution of code under every case label. Otherwise it falls through to the next case. You need to use break, which will jump out of switch:
case '+':
sum = num1 + num2;
cout << "The sum is: " << sum << endl;
break; // <-- end of this case
Put a break; at the end of each case in your switch statement.
You need break statements at the end of each case; otherwise, the execution of the program proceeds with the next case. That's why you see all cases processed when you are handling the + case. The break statement ends execution of the nearest enclosing loop or conditional statement in which it appears.

End1 not declared C++

When I try to build the solution for this program I am receiving the error End1 Not declared for all the end1 statements. Am I missing something?
#include "StdAfx.h
#include <iostream>
using namespace std;
const double PI = 3.14;
int main()
{
double circumference;
double radius;
double area;
cout << "Enter the radius: ";
cin >> radius;
cout << end1;
circumference = 2 * PI * radius;
cout << "Circumference = " << circumference << end1;
area = PI * radius * radius;
cout << "Area = " << area << end1;
return 0;
}
Try replacing the number 1 with the letter l
http://en.cppreference.com/w/cpp/io/manip/endl
replace end1 with endl
cout << "Circumference = " << circumference << endl;

Resources