Click here to Skip to main content
12,299,431 members (57,136 online)
Click here to Skip to main content

Tagged as

Stats

14.6K views
1.2K downloads
13 bookmarked
Posted

A simple graphics library in C with BMP and WMF export

, 15 Jul 2011 CPOL
A graphics library to export graphical output to BMP or WMF.
potential flow
potential flow.vcproj.uhwang-PC.uhwang.user
uniform-dipole.bmp
uniform-rnkovl.bmp
uniform-source.bmp
uniform-vortex.bmp
simple motion
simple motion.vcproj.uhwang-PC.uhwang.user
plate
plate.vcproj.uhwang-PC.uhwang.user
u-plate.bmp
/*

	Simple motion with a = dv/dt = -x
	
	10  REM DY2
	15  REM X-T GRAPH OF DY1
	20  INPUT "SCALE OF T-AXIS ="; K
	30  INPUT "SCALE OF X-AXIS ="; L
	40  HGR : HCOLOR=3
	50  HPLOT 0, 40 TO 0, 140
	60  HPLOT 0, 90 TO 279, 90
	70  X0 = 1:V0 = 0:DT = 0.1
	80  X = X0:V = V0
	90  A = -X
	100 V = V + A * (DT/2)
	110 X = X + V * DT
	120 T = T + DT
	130 T1 = T * K:X1 = X * L
	140 IF T1 > 270 THEN END
	150 HPLOT T1, -X1 + 90
	160 A = -X
	170 V = V + A * DT
	180 GOTO 110
	
	]RUN
	SCALE OF T-AXIS =5
	SCALE OF X-AXIS =40
*/

#include <stdio.h>
#include "libtg.h"

//#define BMP
#define SYMBOL
#define SYMBOL_SKIP
//#define LINE

#define SYMBOL_CIRCLE		0
//#define SYMBOL_DELTA		1
//#define SYMBOL_GRADIENT		2
//#define SYMBOL_RTRIANGLE	3
//#define SYMBOL_LTRIANGLE	4
//#define SYMBOL_DIAMOND		5
//#define SYMBOL_SQUARE		6

int main(void)
{
	float x, v, a, x0=1, v0=0, t=0, dt=0.1, k=5, l=40,x1=0,t1=0;
	float fwid=0, fhgt=5, fratio=0;
	int xwid=279, xhgt=180, skip=5, iskip=0, findex=0;
	TG_Symbol sym_index=TG_SYM_CIRCLE;
	TG_Symbol symbols[] = { TG_SYM_CIRCLE, TG_SYM_DELTA, TG_SYM_GRADIENT, TG_SYM_RTRIANGLE, TG_SYM_LTRIANGLE, TG_SYM_DIAMOND, TG_SYM_SQUARE };
	TG_Color pixel_color = TG_GetColor(TG_BLACK);
	TG_PlotZone_Ptr zone=NULL;
	TG_Workspace_Ptr workspace = TG_InitTGLibrary("Letter");
	
#ifdef BMP
	#ifdef SYMBOL 
		#ifdef SYMBOL_SKIP
		char* filename[] = { 
			"simpmtn-skip-symbol-circle.bmp", 
			"simpmtn-skip-symbol-delta.bmp",
			"simpmtn-skip-symbol-gradient.bmp",
			"simpmtn-skip-symbol-rtriangle.bmp",
			"simpmtn-skip-symbol-ltriangle.bmp",
			"simpmtn-skip-symbol-diamond.bmp",
			"simpmtn-skip-symbol-square.bmp" };
		#else
		char* filename[] = { 
			"simpmtn-symbol-circle.bmp", 
			"simpmtn-symbol-delta.bmp",
			"simpmtn-symbol-gradient.bmp",
			"simpmtn-symbol-rtriangle.bmp",
			"simpmtn-symbol-ltriangle.bmp",
			"simpmtn-symbol-diamond.bmp",
			"simpmtn-symbol-square.bmp" };
		#endif
	#else
		char* filename[] = {"simpmtn.bmp"};
	#endif
#else
	#ifdef SYMBOL
		#ifdef SYMBOL_SKIP
		char* filename[] = { 
			"simpmtn-skip-symbol-circle.wmf", 
			"simpmtn-skip-symbol-delta.wmf",
			"simpmtn-skip-symbol-gradient.wmf",
			"simpmtn-skip-symbol-rtriangle.wmf",
			"simpmtn-skip-symbol-ltriangle.wmf",
			"simpmtn-skip-symbol-diamond.wmf",
			"simpmtn-skip-symbol-square.wmf" };
		#else
		char* filename[] = { 
			"simpmtn-symbol-circle.wmf", 
			"simpmtn-symbol-delta.wmf",
			"simpmtn-symbol-gradient.wmf",
			"simpmtn-symbol-rtriangle.wmf",
			"simpmtn-symbol-ltriangle.wmf",
			"simpmtn-symbol-diamond.wmf",
			"simpmtn-symbol-square.wmf" };
		#endif
	#else
		char* filename[]={ "sympmtn.wmf" };
	#endif
#endif

#ifdef SYMBOL
	#ifdef SYMBOL_CIRCLE
		sym_index = findex = 0;
	#elif defined SYMBOL_DELTA
		sym_index = findex = 1;
	#elif defined SYMBOL_GRADIENT
		sym_index = findex = 2;
	#elif defined SYMBOL_RTRIANGLE
		sym_index = findex = 3;	
	#elif defined SYMBOL_LTRIANGLE
		sym_index = findex = 4;	
	#elif defined SYMBOL_DIAMOND
		sym_index = findex = 5;	
	#elif defined SYMBOL_SQUARE
		sym_index = findex = 6;	
	#endif
#endif

	if(!workspace) return 1;
	
	fratio = (float)xwid/(float)xhgt;
	fwid = fratio*fhgt;
	zone = &workspace->zone;
	TG_SetFrame(&workspace->zone.frame, 0, 0, fwid, fhgt, 0, xwid, 0, xhgt);

#ifdef BMP
	TG_BMP_OpenExport(zone, filename[findex], xwid, xhgt, 1);
#else
	TG_WMF_OpenExport(zone, filename[findex]);
#endif

	x = x0;	
	v = v0;
	a = -x;
	v += a*dt/2;

#ifdef BMP
	TG_BMP_SetReverseWorldCoord(1);
#else	
	TG_WMF_SetReverseWorldCoord(1);
#endif

#ifdef LINE
	zone->plot.makepen(zone, TG_GetColor(TG_BLUE), 0.005);
	zone->plot.moveto(zone, t*k, 90-x*l);
#endif
	
	while(1) {
		x += v*dt;
		t += dt;
		t1 = t*k;
		x1 = x*l;
		if(t1 > 270) break;
		a = -x;
		v += a*dt;
#ifdef LINE
		zone->plot.lineto(zone, t1, -x1+90);
#else
		zone->plot.putpixel1(zone, t1, -x1+90, pixel_color);
#endif

#ifdef SYMBOL
#ifdef SYMBOL_SKIP
		if((iskip%skip)) iskip++;
		else {
#endif
			zone->plot.gsave();
			#ifdef SYMBOL_CIRCLE 
				zone->plot.symbol(zone, t1, -x1+90, symbols[sym_index],.03, TG_GetColor(TG_RED)    ,1 ,TG_GetColor(TG_BLACK),1,0.001);
			#elif defined SYMBOL_DELTA
				zone->plot.symbol(zone, t1, -x1+90, symbols[sym_index],.03, TG_GetColor(TG_GREEN)  ,1 ,TG_GetColor(TG_BLACK),1,0.001);
			#elif defined SYMBOL_GRADIENT
				zone->plot.symbol(zone, t1, -x1+90, symbols[sym_index],.03, TG_GetColor(TG_BLUE)   ,1 ,TG_GetColor(TG_BLACK),1,0.001);
			#elif defined SYMBOL_RTRIANGLE
				zone->plot.symbol(zone, t1, -x1+90, symbols[sym_index],.03, TG_GetColor(TG_YELLOW) ,1 ,TG_GetColor(TG_BLACK),1,0.001);
			#elif defined SYMBOL_LTRIANGLE
				zone->plot.symbol(zone, t1, -x1+90, symbols[sym_index],.03, TG_GetColor(TG_CYAN)   ,1 ,TG_GetColor(TG_BLACK),1,0.001);
			#elif defined SYMBOL_DIAMOND  
				zone->plot.symbol(zone, t1, -x1+90, symbols[sym_index],.03, TG_GetColor(TG_MAGENTA),1 ,TG_GetColor(TG_BLACK),1,0.001);
			#elif defined SYMBOL_SQUARE    
				zone->plot.symbol(zone, t1, -x1+90, symbols[sym_index],.03, TG_GetColor(TG_BROWN)  ,1 ,TG_GetColor(TG_BLACK),1,0.001);
			#endif
			zone->plot.grestore();
#ifdef SYMBOL_SKIP
			iskip=1;
		}
#endif
#endif
	}

#ifdef BMP
    TG_BMP_CloseExport();	
#else
    TG_WMF_CloseExport();
#endif
	TG_CloseTGLibrary(workspace);

	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

hus
United States United States
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160525.2 | Last Updated 15 Jul 2011
Article Copyright 2011 by hus
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid