Polar coordinate/Theta/Midpoint coordinate program

Hello,
Im in a bit of pickle haha, hoping someone can help.
Heres the scenario:

A straight line connects two coordinates (3, 7) and (8, 12) call them (x1, y1) and (x2, y2).

I have an assignment where I need to write a program that calculates the:
Directional factor k = __
Midpoint x coordinate = __
Midpoint y coordinate = __
Straight length is = __
Point (3,7) polar coordinate: r = __ and theta = __
Point (8, 12) polar coordinate: r = __ and theta = __.

The calculations for each are as follows:

Directional factor - (y2-y1)/(x2-x1).

Midpoint x/y - ((x1+x2)/2, (y1+y2)/2).

Straight length - square root of (x1-x2)^2 + (y1-y2)^2)

Polar coordinates r = square root of (x^2 + y^2), theta = arctan(y/x)



Ive barely started on it, but I can see getting problems now with the math part on the last parts and thought maybe someone smarter than I can help me out and I could simply study the code to understand it/learn it better..

I am a beginner at this so please dont judge too hard :D code attached of what I have so far..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <iomanip>
#include <cmath>
#include <math.h>
using namespace std;
int main () {
	int x1 = 3, y1 = 7, x2 = 8, y2 = 12, k, vx, vy, length, polar1, polar2, theta1, theta2;
	k = (y2-y1)/(x2-x1);
	vx = ((x1+x2)/2);
	vy = ((y1+y2)/2);
	
	
	cout <<"The directional factor k is " <<k <<endl;


	cout <<"Midpoint x coordinate is " <<vx <<endl;
	
	
	
	cout <<"Midpoint y coordinate is " <<vy <<endl;
	
	
	
	cout <<"Straight length is " <<length <<endl;



	cout <<"Point (3, 7) polar coordinate: r = " <<polar1 <<"theta = " <<theta1 <<endl;
	
	
	
	cout <<"Point (8, 12) polar coordinate: r = " <<polar2 <<"theta = " <<theta2 <<endl;

return 0;
}
Hello wirelesskill,

There is a good chance that you have not received a reply yet is because of the green check on your post. This tells everyone that you have an answer to your post and most people are likely to pass over it. I think it is possible to edit your post and change it until you have your answer.

You have defined all your variables as "int"s. This will work until you do division. Integer division will drop the decimal portion of the calculation and leave you with the whole number. This may be what you want, but the variables would work better as "double"s giving you more accurate calculations.

On line 7 you have defined your variables. The "x's and "y"s have been given a value and k, vx and vy do not need initialized because you give them a value on the next three lines. Starting from "length" on all these variable are uninitialized. Starting with line 24 on you are trying to display these variable values. On my IDE in the "cout" statements the variables were flagged as a problem. When I compiled the program it gave me warnings in the "cout" statements and at run time the IDE paused the program with another warning.

An alternative to defining your variables could be:
1
2
3
4
double x1 = 3.0, y1 = 7.0, x2 = 8.0, y2 = 12.0;
double k = (y2 - y1) / (x2 - x1);
double vx = ((x1 + x2) / 2);
double vy = ((y1 + y2) / 2);

You could continue on with the variables length{}, polar1{}, polar2{}, theta1{} and ; theta2{}. The {}s after the variable name is the simple way of initializing the variables to zero, but if you define the variables and give them a value, as above, the {}s will not be needed.

This shows you the difference between uninitialized and initialized variables. Also the difference between defining the variables as "int"s and "double"s.


// <--- Uninitialized variables.
The directional factor k is 1
Midpoint x coordinate is 5
Midpoint y coordinate is 9
Straight length is -858993460
Point (3, 7) polar coordinate: r = -858993460 theta = -858993460
Point (8, 12) polar coordinate: r = -858993460 theta = -858993460

// <--- Initialized variables.
The directional factor k is 1
Midpoint x coordinate is 5
Midpoint y coordinate is 9
Straight length is 0
Point (3, 7) polar coordinate: r = 0 theta = 0
Point (8, 12) polar coordinate: r = 0 theta = 0

// <--- Integer division.
The directional factor k is 1
Midpoint x coordinate is 5
Midpoint y coordinate is 9
Straight length is 7  // <--- Done for "length".
Point (3, 7) polar coordinate: r = 0 theta = 0
Point (8, 12) polar coordinate: r = 0 theta = 0

// <--- Double division.
The directional factor k is 1
Midpoint x coordinate is 5.5
Midpoint y coordinate is 9.5
Straight length is 7.07107
Point (3, 7) polar coordinate: r = 0 theta = 0
Point (8, 12) polar coordinate: r = 0 theta = 0

Notice how the midpoint also changed.


The very large negative number may be different on your computer.

I did work up a formula for length, but not the others yet.

Hope this helps,

Andy
Hello wirelesskill,

From the header file for the "length" I used the "sqrt(...)" (square root) and "pow(..., ...)" (power of) functions.

For the "polar" calculations I used the "sqrt" function and it is easier to say "x1 * x1" rather than use the "pow()" function. Slightly quicker and less overhead than the "pow" function.

For arc tangent the function is "atan(...)"

Hope that helps,

Andy
Topic archived. No new replies allowed.