make this code work able instruction are given in picture #include using namespace std; double lerpSingle(double a, double b, double t) { if (t <= 0.5) return a+(b-a)*t; else return b-(b-a)*(1.0-t); } // This is for linear interpolation Vector3 lerp(Vector3 u, Vector3 v, double t) { return Vector3(lerpSingle(u.x, v.x, t), lerpSingle(u.y, v.y, t), lerpSingle(u.z, v.z, t)); } // This is for identifying if 4 vectors are coplanar bool coplanar(Vector3 p, Vector3 q, Vector3 r, Vector3 s) { Vector3 ab = Vector3(q.x-p.x, q.y-p.y, q.z-p.z); Vector3 bc = Vector3(r.x-q.x, r.y-q.y, r.z-q.z); Vector3 cd = Vector3(s.x-r.x, s.y-r.y, s.z-r.z); if (dot(cross(ab,bc), cd) == 0) { return true; } else { return false; } } Below are the helper function I have used for your reference double dot(Vector3 u, Vector3 v) { double product = 0; product = u.x*v.x + u.y*v.y + u.z*v.z; } Vector3 cross(Vector3 u, Vector3 v) { double x = u.y * v.z - u.z * v.y; double y = u.z * v.x - u.x * v.z; double z = u.x * v.y - u.y * v.x; return Vector3(x,y,z); } I have used below helper class #include #include using namespace std; class Vector3 { public: double x; double y; double z; Vector3(double _x, double _y, double _z) { x = _x; y = _y; z = _z; } Vector3 normalized() { double mag = magnitude(); if (mag != 0.) { x /= mag; y /= mag; z /= mag; } return Vector3(x,y,z); } double magnitude() { return abs(sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2))); } }; //my main method int main() { Vector3 p = Vector3(3, 2, -5); Vector3 q = Vector3(-1, 4, -3); Vector3 r = Vector3(-3, 8, -5); Vector3 s = Vector3(-3, 2, 1); bool test = coplanar(p,q,r,s); if (test) { cout << "yes"; } else { cout << "no"; } return 0; } Output: Question No 1: #include #include using namespace std; class Vector3 { //private: public: double x, y, z; Vector3(); Vector3(double x, double y, double z); double dot(Vector3 u, Vector3 v); //dot product Vector3 cross(Vector3 u, Vector3 v); //cross product Vector3 normalized(); //returns a vector pointing in the same direction with length 1 double magnitude(); //returns the length of the vector Vector3 operator+(Vector3 &u); Vector3 operator-(Vector3 &v); Vector3 operator*(double value); double getX(); double getY(); double getZ(); }; Vector3::Vector3() { this->x = 0; this->y = 0; this->z = 0; } Vector3::Vector3(double x, double y, double z) { this->x = x; this->y = y; this->z = z; } Vector3 Vector3::normalized() { double mag = magnitude(); if (mag != 0) { x = x / mag; y = y / mag; z = z / mag; } return Vector3(x, y, z); } double Vector3::magnitude() { double magni = abs(sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2))); return magni; } double Vector3::dot(Vector3 u, Vector3 v) { double product = 0; product = u.x*v.x + u.y*v.y + u.z*v.z; return product; } Vector3 Vector3::cross(Vector3 u, Vector3 v) { double x = u.y * v.z - u.z * v.y; double y = u.z * v.x - u.x * v.z; double z = u.x * v.y - u.y * v.x; return Vector3(x, y, z); } Vector3 Vector3::operator+(Vector3 &v) { double x = this->x + v.x; double y = this->y + v.y; double z = this->z + v.z; return Vector3(x, y, z); } Vector3 Vector3::operator-(Vector3 &v) { double x = this->x - v.x; double y = this->y - v.y; double z = this->z - v.z; return Vector3(x, y, z); } Vector3 Vector3::operator*(double value) { double x = this->x * value; double y = this->y * value; double z = this->z * value; return Vector3(x, y, z); } double Vector3::getX() { return this->x; } double Vector3::getY() { return this->y; } double Vector3::getZ() { return this->z; } Vector3 getOriginalVector(Vector3 vertices[], int numVertices) { Vector3 vec(0, 0, 0); Vector3 vect1, vect2, vect3; for (int i = 0; i < numVertices-2; i++) { Vector3 v1 = vertices[i]; Vector3 v2 = vertices[(i + 1) % numVertices]; vect1 = (vertices[i + 1] - vertices[i]); vect2 = (vertices[i + 2] - vertices[i + 1]); vect3 = (vec.cross(vect1, vect2)); vec = vec + vect3; } vect1 = (vertices[numVertices - 1] - vertices[numVertices - 2]); vect2 = (vertices[0] - vertices[numVertices - 1]); vect3 = (vec.cross(vect1, vect2)); vec = vec + vect3; return vec.normalized(); } int main() { int const numVertices = 5; Vector3 vertices[numVertices] = { Vector3(2.0, 0.0, 0.0), Vector3(2.0, 2.0, 0.0), Vector3(1.0, 2.0, 0.0), Vector3(0.0, 2.0, 0.0), Vector3(0.0, 0.0, 2.0) }; Vector3 n = getOriginalVector(vertices, numVertices); cout << "originalVector = (" << n.getX() << ", " << n.getY() << ", " << n.getZ() << ")\n"; return 0; }
make this code work able instruction are given in picture
#include<math.h>
using namespace std;
double lerpSingle(double a, double b, double t)
{
if (t <= 0.5)
return a+(b-a)*t;
else
return b-(b-a)*(1.0-t);
}
// This is for linear interpolation
Vector3 lerp(Vector3 u, Vector3 v, double t) {
return Vector3(lerpSingle(u.x, v.x, t), lerpSingle(u.y, v.y, t), lerpSingle(u.z, v.z, t));
}
// This is for identifying if 4
bool coplanar(Vector3 p, Vector3 q, Vector3 r, Vector3 s) {
Vector3 ab = Vector3(q.x-p.x, q.y-p.y, q.z-p.z);
Vector3 bc = Vector3(r.x-q.x, r.y-q.y, r.z-q.z);
Vector3 cd = Vector3(s.x-r.x, s.y-r.y, s.z-r.z);
if (dot(cross(ab,bc), cd) == 0) {
return true;
} else {
return false;
}
}
Below are the helper function I have used for your reference
double dot(Vector3 u, Vector3 v) {
double product = 0;
product = u.x*v.x + u.y*v.y + u.z*v.z;
}
Vector3 cross(Vector3 u, Vector3 v) {
double x = u.y * v.z - u.z * v.y;
double y = u.z * v.x - u.x * v.z;
double z = u.x * v.y - u.y * v.x;
return Vector3(x,y,z);
}
I have used below helper class
#include <iostream>
#include <math.h>
using namespace std;
class Vector3 {
public:
double x;
double y;
double z;
Vector3(double _x, double _y, double _z) {
x = _x;
y = _y;
z = _z;
}
Vector3 normalized() {
double mag = magnitude();
if (mag != 0.) {
x /= mag;
y /= mag;
z /= mag;
}
return Vector3(x,y,z);
}
double magnitude() {
return abs(sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2)));
}
};
//my main method
int main()
{
Vector3 p = Vector3(3, 2, -5);
Vector3 q = Vector3(-1, 4, -3);
Vector3 r = Vector3(-3, 8, -5);
Vector3 s = Vector3(-3, 2, 1);
bool test = coplanar(p,q,r,s);
if (test) {
cout << "yes";
} else {
cout << "no";
}
return 0;
}
Output:
Question No 1:
#include <iostream>
#include<math.h>
using namespace std;
class Vector3
{
//private:
public:
double x, y, z;
Vector3();
Vector3(double x, double y, double z);
double dot(Vector3 u, Vector3 v); //dot product
Vector3 cross(Vector3 u, Vector3 v); //cross product
Vector3 normalized(); //returns a vector pointing in the same direction with length 1
double magnitude(); //returns the length of the vector
Vector3 operator+(Vector3 &u);
Vector3 operator-(Vector3 &v);
Vector3 operator*(double value);
double getX();
double getY();
double getZ();
};
Vector3::Vector3()
{
this->x = 0;
this->y = 0;
this->z = 0;
}
Vector3::Vector3(double x, double y, double z)
{
this->x = x;
this->y = y;
this->z = z;
}
Vector3 Vector3::normalized()
{
double mag = magnitude();
if (mag != 0)
{
x = x / mag;
y = y / mag;
z = z / mag;
}
return Vector3(x, y, z);
}
double Vector3::magnitude()
{
double magni = abs(sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2)));
return magni;
}
double Vector3::dot(Vector3 u, Vector3 v)
{
double product = 0;
product = u.x*v.x + u.y*v.y + u.z*v.z;
return product;
}
Vector3 Vector3::cross(Vector3 u, Vector3 v)
{
double x = u.y * v.z - u.z * v.y;
double y = u.z * v.x - u.x * v.z;
double z = u.x * v.y - u.y * v.x;
return Vector3(x, y, z);
}
Vector3 Vector3::operator+(Vector3 &v)
{
double x = this->x + v.x;
double y = this->y + v.y;
double z = this->z + v.z;
return Vector3(x, y, z);
}
Vector3 Vector3::operator-(Vector3 &v)
{
double x = this->x - v.x;
double y = this->y - v.y;
double z = this->z - v.z;
return Vector3(x, y, z);
}
Vector3 Vector3::operator*(double value)
{
double x = this->x * value;
double y = this->y * value;
double z = this->z * value;
return Vector3(x, y, z);
}
double Vector3::getX()
{
return this->x;
}
double Vector3::getY()
{
return this->y;
}
double Vector3::getZ()
{
return this->z;
}
Vector3 getOriginalVector(Vector3 vertices[], int numVertices)
{
Vector3 vec(0, 0, 0);
Vector3 vect1, vect2, vect3;
for (int i = 0; i < numVertices-2; i++)
{
Vector3 v1 = vertices[i];
Vector3 v2 = vertices[(i + 1) % numVertices];
vect1 = (vertices[i + 1] - vertices[i]);
vect2 = (vertices[i + 2] - vertices[i + 1]);
vect3 = (vec.cross(vect1, vect2));
vec = vec + vect3;
}
vect1 = (vertices[numVertices - 1] - vertices[numVertices - 2]);
vect2 = (vertices[0] - vertices[numVertices - 1]);
vect3 = (vec.cross(vect1, vect2));
vec = vec + vect3;
return vec.normalized();
}
int main()
{
int const numVertices = 5;
Vector3 vertices[numVertices] = { Vector3(2.0, 0.0, 0.0), Vector3(2.0, 2.0, 0.0),
Vector3(1.0, 2.0, 0.0), Vector3(0.0, 2.0, 0.0),
Vector3(0.0, 0.0, 2.0) };
Vector3 n = getOriginalVector(vertices, numVertices);
cout << "originalVector = (" << n.getX() << ", " << n.getY() << ", " << n.getZ() << ")\n";
return 0;
}
Step by step
Solved in 2 steps with 1 images