-
Help with structs...
Hi, I have to do an assignment(explained in code) and I keep getting this error:
PHP Code:
In function 'int main()':
error: conversion from 'Point' to non-scalar type 'MathVector' requested
(The little red x is at the last line in main)
Can someone please tell me what I need to change in order to make this program work. Thank you.
Code:
// This program reads and stores start and end coordinates of two vectors// and then determines if the vectors are orthogonal or not using the
// dot product equation
// including required libraries
#include <iostream>
#include <cmath>
// using namespace
using namespace std;
// struct definitions & declarations
struct Point
{
double x;
double y;
double z;
};
struct MathVector
{
Point start;
Point end;
};
// Function prototypes
Point ReadPoint();
double DistanceBetweenPoints( const Point& pt1, const Point& pt2 );
void Dot_Product ( MathVector sa, MathVector ea, MathVector sb, MathVector eb);
// main function
int main()
{
Point starta = ReadPoint();
Point enda = ReadPoint();
Point startb = ReadPoint();
Point endb = ReadPoint();
Dot_Product (starta, enda, startb, endb);
}
// function definition
Point ReadPoint()
{
double x1;
double y1;
double z1;
cout << "Enter the coordinates x, y, and z of the point: ";
cin >> x1 >> y1 >> z1;
Point pt1;
pt1.x=x1;
pt1.y=y1;
pt1.z=z1;
return pt1;
}
double DistanceBetweenPoints( const Point& pt1, const Point& pt2 )
{
double x1= pt1.x;
double x2= pt2.x;
double y1= pt1.y;
double y2= pt2.y;
double z1= pt1.z;
double z2= pt2.y;
double distance = sqrt(pow((x1-x2),2)+pow((y1-y2),2)+pow((z1-z2),2));
return distance;
}
void Dot_Product ( MathVector sa, MathVector ea, MathVector sb, MathVector eb)
{
double x_starta = sa.start.x;
double x_enda = ea.end.x;
double x_startb = sb.start.x;
double x_endb = eb.end.x;
double y_starta = sa.start.y;
double y_enda = ea.end.y;
double y_startb = sb.start.y;
double y_endb = eb.end.y;
double z_starta = sa.start.z;
double z_enda = ea.end.z;
double z_startb = sb.start.z;
double z_endb = eb.end.z;
double product = (x_enda-x_starta)*(x_endb-x_startb)+ (y_enda-y_starta)*(y_endb-y_startb)+(z_enda-z_starta)*(z_endb-z_startb);
if (product == 0)
cout << "The vectors are orthogonal.";
else
cout << "The vectors are not orthogonal.";
return;
}
-
You declared your dot product function to take 4 vectors but then pass in 4 points. That's the syntactical error. From a mathematical point of view I don't understand your code. A mathematical vector is nothing but a point with a different definition. (4/2/17) is a vector. A vector is not defined by two points because that would limit it to exactly one start and one end. A dot product takes two vectors. Not 4.
-
You know, it probably doesn't matter a lot but, a vector *isn't* a line segment!
What you've tried to build is a line segment, with a start and an end. A mathematical vector has only direction and magnitude, not position, thus the possible ways to represent it is by either a n-sized column holding the "cartesian" coordinates of the origin added with it, or a (n-1)-sized column holding its "polar" coordinates and nth element for lenght.
( Just to keep heads spinning... :p )