// code to implement the point (mass) class in our mass-spring simulation // Date: 10/17/08 // Author: CSCI 323 #include "point.h" #include #include #include using namespace std; extern point *jello; extern int NPTS; extern double elapsed; point::point(void) { MASS = 0.01; SRL = 0.05; k = 1.0; mass = MASS; Fx = Fy = 0.0; vx = vy = 0.0; return; } point::~point(void) { return; } double point::getx(void) { return x; } double point::gety(void) { return y; } void point::setx(double u) { x = u; } void point::sety(double v) { y = v; } double point::getnewx(void) { return newx; } double point::getnewy(void) { return newy; } void point::setnewx(double u) { newx = u; } void point::setnewy(double v) { newy = v; } void point::calcForces(void) { int index = this - jello; double F, u, v, dist; // initialize forces (and put in gravity) Fx = 0.0; // Fy = -9.8*mass; Fy = -0.1; // calculate horizontal and vertical forces if ((index - NPTS)>=0) // compute forces from row below because not bottom of square { // find distance between current point and current point - NPTS dist = distance(this - NPTS); // subtract SRL to find displacement dist = dist - SRL; // compute force as k times displacement F = -(k*dist); // compute vector that is difference of 2 points u = x - (this - NPTS)->x; v = y - (this - NPTS)->y; // normalize vector normalize(u, v); // scale by force u *= F; v *= F; // add components to Fx, Fy Fx += u; Fy += v; } if ((index+NPTS) < NPTS*NPTS) // compute forces from row above because not top of square { // find distance between current point and current point - NPTS dist = distance(this + NPTS); // subtract SRL to find displacement dist = dist - SRL; // compute force as k times displacement F = -(k*dist); // compute vector that is difference of 2 points u = x - (this + NPTS)->x; v = y - (this + NPTS)->y; // normalize vector normalize(u, v); // scale by force u *= F; v *= F; // add components to Fx, Fy Fx += u; Fy += v; } if ((index%NPTS) != 0) // compute forces from left because not at left edge of square { // find distance between current point and current point - NPTS dist = distance(this - 1); // subtract SRL to find displacement dist = dist - SRL; // compute force as k times displacement F = -(k*dist); // compute vector that is difference of 2 points u = x - (this - 1)->x; v = y - (this - 1)->y; // normalize vector normalize(u, v); // scale by force u *= F; v *= F; // add components to Fx, Fy Fx += u; Fy += v; } if ((index%NPTS) != NPTS - 1) // compute forces from right because not at right edge of square { // find distance between current point and current point - NPTS dist = distance(this + 1); // subtract SRL to find displacement dist = dist - SRL; // compute force as k times displacement F = -(k*dist); // compute vector that is difference of 2 points u = x - (this + 1)->x; v = y - (this + 1)->y; // normalize vector normalize(u, v); // scale by force u *= F; v *= F; // add components to Fx, Fy Fx += u; Fy += v; } // calculate diagonal forces if ((index - NPTS)>=0) // compute forces from row below because not bottom of square { if ((index%NPTS) != 0) // compute forces from left because not at left edge of square { // find distance between current point and current point - NPTS dist = distance(this - NPTS - 1); // subtract SRL to find displacement dist = dist - SRL*1.414; // compute force as k times displacement F = -(k*dist); // compute vector that is difference of 2 points u = x - (this - NPTS - 1)->x; v = y - (this - NPTS - 1)->y; // normalize vector normalize(u, v); // scale by force u *= F; v *= F; // add components to Fx, Fy Fx += u; Fy += v; } if ((index%NPTS) != NPTS - 1) // compute forces from right because not at right edge of square { // find distance between current point and current point - NPTS dist = distance(this - NPTS + 1); // subtract SRL to find displacement dist = dist - SRL*1.414; // compute force as k times displacement F = -(k*dist); // compute vector that is difference of 2 points u = x - (this - NPTS + 1)->x; v = y - (this - NPTS + 1)->y; // normalize vector normalize(u, v); // scale by force u *= F; v *= F; // add components to Fx, Fy Fx += u; Fy += v; } } if ((index+NPTS) < NPTS*NPTS) // compute forces from row above because not top of square { if ((index%NPTS) != 0) // compute forces from left because not at left edge of square { // find distance between current point and current point - NPTS dist = distance(this + NPTS - 1); // subtract SRL to find displacement dist = dist - SRL*1.414; // compute force as k times displacement F = -(k*dist); // compute vector that is difference of 2 points u = x - (this + NPTS - 1)->x; v = y - (this + NPTS - 1)->y; // normalize vector normalize(u, v); // scale by force u *= F; v *= F; // add components to Fx, Fy Fx += u; Fy += v; } if ((index%NPTS) != NPTS - 1) // compute forces from right because not at right edge of square { // find distance between current point and current point - NPTS dist = distance(this + NPTS + 1); // subtract SRL to find displacement dist = dist - SRL*1.414; // compute force as k times displacement F = -(k*dist); // compute vector that is difference of 2 points u = x - (this + NPTS + 1)->x; v = y - (this + NPTS + 1)->y; // normalize vector normalize(u, v); // scale by force u *= F; v *= F; // add components to Fx, Fy Fx += u; Fy += v; } } if (index == 0) { // cout << "Fx = " << Fx << " Fy = " << Fy << endl; // cout << "elapsed time = " << elapsed << endl; } return; } void point::calcNewPos(void) { double ax, ay; ax = Fx/mass; ay = Fy/mass; vx = vx + ax*elapsed; vy = vy + ay*elapsed; newx = x + vx*elapsed; newy = y + vy*elapsed; if (newx < -1.0) { vx = -vx; newx = -1.0; } if (newx > 1.0) { vx = -vx; newx = 1.0; } if (newy < -0.5) { vy = -vy; newy = -0.5; } if (newy > 1.0) { vy = -vy; newy = 1.0; } if (this - jello == 0) { cout << "ax = " << ax << " ay = " << ay << endl; cout << "vx = " << vx << " vy = " << vy << endl; cout << "newx = " << newx << " newy = " << newy << endl; cout << "x = " << x << " y = " << y << endl << endl; } return; } double point::distance(point *b) { return sqrt((this->x - b->x) * (this->x - b->x) + (this->y - b->y) * (this->y - b->y)); } void point::normalize(double &x, double &y) { double d = sqrt(x*x + y*y); x /= d; y /= d; return; }