void ForceDirectedGraphDrawing::computeAttractiveForces(std::shared_ptr<GraphClass>g)
{
    MDtype k = 0.1 * sqrt((*HEIGHT * *WIDTH) / (1.0 + g->vertices->size()));
    for(unsigned int i = 0; i < g->edges->size(); ++i){
        std::shared_ptr<VertexClass> v1 = g->edges->at(i)->A;
        std::shared_ptr<VertexClass> v2 = g->edges->at(i)->B;
        MDtype distance = getDistanceBetweenVertex(v1, v2);
        MDtype attractiveF = distance*distance / k * *ATTRACTION;
        if (distance > 0) {
            v1->xForce += (getDx(v1, v2) / distance) * attractiveF;
            v1->yForce += (getDy(v1, v2) / distance) * attractiveF;
            v2->xForce -= (getDx(v1, v2) / distance) * attractiveF;
            v2->yForce -= (getDy(v1, v2) / distance) * attractiveF;
        }
    }
}
Example #2
0
void Player::Update(HGE* hge) {
    float dt=hge->Timer_GetDelta();
    if(x>784) {
        x=784-(x-784);
        dx=-dx;
    }
    if(x<16) {
        x=16+16-x;
        dx=-dx;
    }
    if(y>584) {
        y=584-(y-584);
        dy=-dy;
    }
    if(y<16) {
        y=16+16-y;
        dy=-dy;
    }
    // Process keys
    if (hge->Input_KeyDown(HGEK_SPACE)) this->Rotate();
    if (hge->Input_GetKeyState(key) && simpleRotation==0) setDx(getDx()+getSpeed()*dt);
    if (hge->Input_GetKeyState(key) && simpleRotation==1) setDy(getDy()+getSpeed()*dt);
    if (hge->Input_GetKeyState(key) && simpleRotation==2) setDx(getDx()-getSpeed()*dt);
    if (hge->Input_GetKeyState(key) && simpleRotation==3) setDy(getDy()-getSpeed()*dt);
    if (invulnerableTimer > 0) {
        invulnerableTimer -= dt;
        displayShipTimer -= dt;
        if (displayShipTimer < 0) {
            displayShipTimer = 0.25f;
            if (displayShip) displayShip = false;
            else displayShip = true;
        }
    }
    else displayShip = true;
    Ship::Update(hge);
}
void ForceDirectedGraphDrawing::computeRepulsiveForces(std::shared_ptr<GraphClass> g)
{
    MDtype k = sqrt((*WIDTH * *HEIGHT) / (1.0 + g->vertices->size()));
    for(unsigned int i = 0; i < g->vertices->size(); ++i){
        for(unsigned int j = 0; j < g->vertices->size(); ++j){
            if(i != j){
                std::shared_ptr<VertexClass> v1 = g->vertices->at(i);
                std::shared_ptr<VertexClass> v2 = g->vertices->at(j);
                MDtype distance = getDistanceBetweenVertex(v1, v2);
                if (distance > 0) {
                    MDtype repulsiveF = k*k / distance * *REPULSION;
                    v1->xForce -= (( getDx(v1,v2) / distance) * repulsiveF);
                    v1->yForce -= (( getDy(v1,v2) / distance) * repulsiveF);
                }
            }
        }
    }
}
MDtype GetDistance::getDistanceBetweenVertex(std::shared_ptr<VertexClass> v1, std::shared_ptr<VertexClass> v2) {
    MDtype dx = getDx(v1, v2);
    MDtype dy = getDy(v1, v2);
    return sqrt(dx*dx + dy*dy);
}
Example #5
0
slug getslug(FILE *fp)
{
	if (inbuf == NULL) {
		if ((inbuf = (char *)malloc(ninbuf = DELTABUF)) == NULL)
			ERROR "no room for %ld character input buffer\n", ninbuf FATAL;
		inbp = inbuf;
	}
	if (wherebuf() > (ssize_t)(ninbuf-5000)) {
		// this is still flaky -- lines can be very long
		int where = wherebuf();	// where we were
		if ((inbuf = (char *)realloc(inbuf, ninbuf += DELTABUF)) == NULL)
			ERROR "no room for %ld character input buffer\n", ninbuf FATAL;
		ERROR "grew input buffer to %ld characters\n", ninbuf WARNING;
		inbp = inbuf + where;	// same offset in new array
	}
	static int baseV = 0;	// first V command of preceding slug
	static int curV = 0, curH = 0;
	static int font = 0, size = 0;
	static int baseadj = 0;
	static int ncol = 1, offset = 0;	// multi-column stuff
	char str[1000], str2[1000], buf[3000], *p;
	int firstV = 0, firstH = 0;
	int maxV = curV;
	int ocurV = curV, mxv = 0, dx = 0;
	int sawD = 0;		// > 0 if have seen D...
	slug ret;
	ret.serialnum = serialnum++;
	ret.type = VBOX;	// use the same as last by default
	ret.dv = curV - baseV;
	ret.hpos = curH;
	ret.base =  ret.parm = ret.parm2 = ret.seen = 0;
	ret.font = font;
	ret.size = size;
	ret.dp = wherebuf();
	ret.ncol = ncol;
	ret.offset = offset;
	ret.linenum = linenum;	// might be low

	for (;;) {
		int c, m, n;	// for input values
		int sign;		// hoisted from case 'h' below
		switch (c = getc(fp)) {
		case EOF:
			ret.type = EOF;
			ret.dv = 0;
			if (baseadj)
				printf("# adjusted %d bases\n", baseadj);
			printf("# %d characters, %d lines\n", wherebuf(), linenum);
			return ret;
		case 'V':
			fscanf(fp, "%d", &n);
			if (firstV++ == 0) {
				ret.dv = n - baseV;
				baseV = n;
			} else {
				sprintf(buf, "v%d", n - curV);
				adds(buf);
			}
			curV = n;
			maxV = max(maxV, curV);
			break;
		case 'H':		// absolute H motion
			fscanf(fp, "%d", &n);
			if (firstH++ == 0) {
				ret.hpos = n;
			} else {
				sprintf(buf, "h%d", n - curH);
				adds(buf);
			}
			curH = n;
			break;
		case 'h':		// relative H motion
			addc(c);
			sign = 1;
			if ((c = getc(fp)) == '-') {
				addc(c);
				sign = -1;
				c = getc(fp);
			}
			for (n = 0; isdigit(c); c = getc(fp)) {
				addc(c);
				n = 10 * n + c - '0';
			}
			curH += n * sign;
			ungetc(c, fp);
			break;
		case 'x':	// device control: x ...
			addc(c);
			fgets(buf, sizeof(buf), fp);
			linenum++;
			adds(buf);
			if (buf[0] == ' ' && buf[1] == 'X') {	// x X ...
				if (2 != sscanf(buf+2, "%s %d", str, &n))
					n = 0;
				if (eq(str, "SP")) {	// X SP n
					ret.type = SP;	// paddable SPace
					ret.dv = n;	// of height n
				} else if (eq(str, "BS")) {
					ret.type = BS;	// Breakable Stream
					ret.parm = n;	// >=n VBOXES on a page
				} else if (eq(str, "BF")) {
					ret.type = BF;	// Breakable Float
					ret.parm = ret.parm2 = n;
							// n = pref center (as UF)
				} else if (eq(str, "US")) {
					ret.type = US;	// Unbreakable Stream
					ret.parm = n;
				} else if (eq(str, "UF")) {
					ret.type = UF;	// Unbreakable Float
					ret.parm = ret.parm2 = n;
							// n = preferred center
							// to select several,
							// use several UF lines
				} else if (eq(str, "PT")) {
					ret.type = PT;	// Page Title
					ret.parm = n;
				} else if (eq(str, "BT")) {
					ret.type = BT;	// Bottom Title
					ret.parm = n;
				} else if (eq(str, "END")) {
					ret.type = END;
					ret.parm = n;
				} else if (eq(str, "TM")) {
					ret.type = TM;	// Terminal Message
					ret.dv = 0;
				} else if (eq(str, "COORD")) {
					ret.type = COORD;// page COORDinates
					ret.dv = 0;
				} else if (eq(str, "NE")) {
					ret.type = NE;	// NEed to break page
					ret.dv = n;	// if <n units left
				} else if (eq(str, "MC")) {
					ret.type = MC;	// Multiple Columns
					sscanf(buf+2, "%s %d %d",
						str, &ncol, &offset);
					ret.ncol = ncol;
					ret.offset = offset;
				} else if (eq(str, "CMD")) {
					ret.type = CMD;	// CoMmaNd
					sscanf(buf+2, "%s %s", str2, str);
					if (eq(str, "FC"))	// Freeze 2-Col
						ret.parm = FC;
					else if (eq(str, "FL"))	// FLush
						ret.parm = FL;
					else if (eq(str, "BP"))	// Break Page
						ret.parm = BP;
					else ERROR "unknown command %s\n",
						str WARNING;
				} else if (eq(str, "PARM")) {
					ret.type = PARM;// PARaMeter
					sscanf(buf+2, "%s %s %d", str2, str, &ret.parm2);
					if (eq(str, "NP"))	// New Page
						ret.parm = NP;
					else if (eq(str, "FO"))	// FOoter
						ret.parm = FO;
					else if (eq(str, "PL")) // Page Length
						ret.parm = PL;
					else if (eq(str, "MF")) // MinFull
						ret.parm = MF;
					else if (eq(str, "CT")) // ColTol
						ret.parm = CT;
					else if (eq(str, "WARN")) //WARNings?
						ret.parm = WARN;
					else if (eq(str, "DBG"))// DeBuG
						ret.parm = DBG;
					else ERROR "unknown parameter %s\n",
						str WARNING;
				} else
					break;		// out of switch
				if (firstV > 0)
					ERROR "weird x X %s in mid-VBOX\n",
						str WARNING;
				return ret;
			}
			break;
		case 'n':	// end of line
			fscanf(fp, "%d %d", &n, &m);
			ret.ht = n;
			ret.base = m;
			getc(fp);	// newline
			linenum++;
			sprintf(buf, "n%d %d\n", ret.ht, ret.base);
			adds(buf);
			if (!firstV++)
				baseV = curV;
			// older incarnations of this program used ret.base
			// in complicated and unreliable ways;
			// example:  if ret.ht + ret.base < ret.dv, ret.base = 0
			// this was meant to avoid double-counting the space
			// around displayed equations; it didn't work
			// Now, we believe ret.base = 0, otherwise we give it
			// a value we have computed.
			if (ret.base == 0 && sawD == 0)
				return ret;	// don't fiddle 0-bases
			if (ret.base != maxV - baseV) {
				ret.base = maxV - baseV;
				baseadj++;
			}
			if (ret.type != VBOX)
				ERROR "%s slug (type %d) has base = %d\n",
					ret.type_name(), ret.type, ret.base WARNING;
			return ret;
		case 'p':	// new page
			fscanf(fp, "%d", &n);
			ret.type = PAGE;
			curV = baseV = ret.dv = 0;
			ret.parm = n;	// just in case someone needs it
			return ret;
		case 's':	// size change snnn
			fscanf(fp, "%d", &size);
			sprintf(buf, "s%d\n", size);
			adds(buf);
			break;
		case 'f':	// font fnnn
			fscanf(fp, "%d", &font);
			sprintf(buf, "f%d\n", font);
			adds(buf);
			break;
		case '\n':
			linenum++;
			/* fall through */
		case ' ':
			addc(c);
			break;
		case '0': case '1': case '2': case '3': case '4':
		case '5': case '6': case '7': case '8': case '9':
			// two motion digits plus a character
			addc(c);
			n = c - '0';
			addc(c = getc(fp));
			curH += 10 * n + c - '0';
			addc(getc(fp));
			if (!firstV++)
				baseV = curV;
			break;
		case 'c':	// single ascii character
			addc(c);
			addc(getc(fp));
			if (!firstV++)
				baseV = curV;
			break;
		case 'C':	// Cxyz\n
		case 'N':	// Nnnn\n
			addc(c);
			while ((c = getc(fp)) != ' ' && c != '\n')
				addc(c);
			addc(c);
			if (!firstV++)
				baseV = curV;
			linenum++;
			break;
		case 'D':	// draw function: D.*\n
			sawD++;
			p = bufptr(wherebuf());	// where does the D start
			addc(c);
			while ((c = getc(fp)) != '\n')
				addc(c);
			addc(c);
			if (!firstV++)
				baseV = curV;
			ocurV = curV, mxv = 0, dx = 0;
			curV += getDy(p, &dx, &mxv);	// figure out how big it is
			maxV = max(max(maxV, curV), ocurV+mxv);
			curH += dx;
			linenum++;
			break;
		case 'v':	// relative vertical vnnn
			addc(c);
			if (!firstV++)
				baseV = curV;
			sign = 1;
			if ((c = getc(fp)) == '-') {
				addc(c);
				sign = -1;
				c = getc(fp);
			}
			for (n = 0; isdigit(c); c = getc(fp)) {
				addc(c);
				n = 10 * n + c - '0';
			}
			ungetc(c, fp);
			curV += n * sign;
			maxV = max(maxV, curV);
			addc('\n');
			break;
		case 'w':	// word space
			addc(c);
			break;
		case '#':	// comment
			addc(c);
			while ((c = getc(fp)) != '\n')
				addc(c);
			addc('\n');
			linenum++;
			break;
		default:
			ERROR "unknown input character %o %c (%50.50s)\n",
				c, c, bufptr(wherebuf()-50) WARNING;
			break;
		}
	}
}