reading file, vector magnitude, angles

so i have to write a program that reads x and y coordinates from a file, calculates vector lengths and positive radian and degree angles, and writes the data to a file which includes the x, y points plus the calculated data.

i get how to get the x and y coordinates from the file, but how do you plug those into finding the magnitude of the vector then finding the angles

here is what i have so far

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cmath>

using namespace std;
double magnitude(double x, double y);
double angle(double x, double y);

int main()
{
double x=0,y=0;
ofstream datafile1(datafile1.dat);
datafile1 >> x >> y;

datafile1 << line;


}
You need help with the formula or to convert the formula into c++ syntax?
no i need to know how to take the x and y coordinates from the file and plug them into the magnitude formula. im thinkin i assign those a variable such as x = a, y= b?
You are already taking the x, y from file. Just have another variable and assign the result of the formula to that variable.
Something like double result = x*y+ 2 (or whatever the formula is) and print the value.
sweet thanks dude. ill have more questions later haha
The code above doesn't compile, it can't be what was actually tried.
But it is attempting to both read and write from/ to an output file, which won't work, unless it is opened for input/output.
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
#include <iostream>
#include <fstream>

    using namespace std;

int main()
{
    double x = 1.2;
    double y = 3.4;

    // Write out the data
    ofstream datafile1("datafile1.dat");
    datafile1 << x << ' ' << y << endl;
    datafile1.close();

    // Read in the data
    x = 0;
    y = 0;
    ifstream datafile2("datafile1.dat");
    datafile2 >> x >> y;
    datafile2.close();

    // and display it
    cout << "x: " << x << "  y: " << y << endl;

}
Last edited on
heres what i have so far. i know the bottom 1/3 is wrong...but any suggestions on what to change would be awesome

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cmath>

using namespace std;
double vector(double x, double y);
double angle(double x, double y);

int main()
{
double x=0,y=0;
// Open the data file and read the text
ofstream datafile1("datafile1.dat");
datafile1 << x << " " << y;
datafile1.close(); // close data file


double vector;
double xv;
double yv;
xv = 0;
yv = 0;
double angle (double a, double b);
double sqrt (xv * xv + yv * yv);
double rad;
double deg;
rad = 0;
deg = 0;

// Print results
cout << sqrt << endl;
cout << 180 * rad / 3.14 << endl;
cout << 180 * deg / 3.14 << endl;


}
I've absolutely no idea what it is you want to achieve.
This is your code...
1
2
3
4
// Open the data file and read the text
ofstream datafile1("datafile1.dat");
datafile1 << x << " " << y;
datafile1.close(); // close data file 

The comment says it is reading some text. But the actual code is writing out two values.

Which is it you are aiming to do?
Do you understand how to work out the angle between two vectors/magnitude of a vector. You will need functions to get the magnitude, find the angle and work out the vector between the co ordinates you are reading in. This is what I made, your solution would be different to account of the reading/writing of your file:

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <cmath>
#include <iostream>

#ifndef PI
#define PI 3.14159265
#endif

using std::cout;
using std::endl;

struct Vector{float i,j,k;}; //vector struct
struct Point{float x,y,z;}; //cord struct
//functions
float GetAngle(Vector v1, Vector v2, float *a, float *b);
float Magnitude(Vector v1);
Vector GetVec(Point a, Point b);

int main()
{
	//create two vectors to compare
	Vector myVec1 = {0};
	Vector myVec2 = {0};

	//create two sets of points (you would read them in)
	Point a1 = {1,5,-7};
	Point a2 = {-2,12,19};
	Point b1 = {12,-9,1};
	Point b2 = {0,-7,1};

	//find out the vectors between the points
	myVec1 = GetVec(a1,a2);
	myVec2 = GetVec(b1,b2);

	//create floats to hold the magnitudes of the two vectors
	float magnitude1 = 0.f;
	float magnitude2 = 0.f;

	//create variable to store the angle
	float angle = 0.f;
	//work out the angle
	angle = GetAngle(myVec1, myVec2, &magnitude1, &magnitude2);
	//output results (you would write this to your file)
	cout << angle << endl;
	cout << magnitude1 << endl;
	cout << magnitude2 << endl;
        
        return 0;

}

float GetAngle(Vector v1, Vector v2, float *a, float *b)
{
	float tempAngle;
	float DotProduct;

	//store the magnitude values via our pointers
	*a = Magnitude(v1);
	*b = Magnitude(v2);
	//work out the dot product of the two vectors
	DotProduct = ((v1.i * v2.i)+(v1.j*v2.j)+(v1.k * v2.k));
	//angle = inv cosine of dot product divided the the product of the magnitude of vector a and vector b
	tempAngle = acos(DotProduct/((*a) * (*b)));

	//return the angle in degrees
	return tempAngle*180/PI;
}
float Magnitude(Vector v1)
{
	return sqrt(pow(v1.i, 2) + pow(v1.j, 2) + pow(v1.k, 2)); //return the magnitude (pythagoras)
}
Vector GetVec(Point a, Point b)
{
	Vector temp = {0};

	temp.i = b.x - a.x;
	temp.j = b.y - a.y;
	temp.k = b.z - a.z;

	return temp; //return vector between two points
}


Hope this helps. If you decide to use this make sure you go through it and understand it.
Last edited on
sorry guys...im pretty new at this still. thanks for writing your own code maritanxx, but i like to write my own, but i will be taking bits, pieces and suggestions from yours

thans for the help guys
thats okay, its much better if you write your own code :) Hopefully mine can give you a starting point.
Topic archived. No new replies allowed.