⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 draw.cpp

📁 课题研究用的直线型一级倒立摆的源程序
💻 CPP
字号:
#include <dos.h>
#include <graphics.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "chinese.h"
#include "ctrl.h"
#include "face.h"
#include "draw.h"
#include "sv_pci.h"

extern double angle;
extern double angleDot;
extern double ang_2pi;
extern double pos;
extern double posDot;
extern double zoom_the, zoom_thedot, zoom_pos, zoom_posdot;
extern void *image;

int old_left = BASE_X+165, old_top = BASE_Y-20;
int old_right = BASE_Y+190, old_bottom = BASE_Y+70;

extern int start;
extern int motion;
extern int safety;
extern int pend;

DRAW_POINT dp[MAX_POINT];
int point_index = 0;

void my_put_pixel(int x, int y, int color)
{
	unsigned char far *p;
	unsigned char q, r;

	outportb(0x3ce,0x05);
	outportb(0x3cf,0x02);
	outportb(0x3ce,0x08);

	p=(unsigned char far *)(0x0a0000000L+ 80L * (long)y + (long)(x / 8));
	q = x % 8;
	q = MASK >> q;
	outportb(0x3cf,q);
	r=*p;
	*p=color;

	outportb(0x3ce,0x0005);
	outportb(0x3ce,0xff08);
}

void draw_text()
{
	double temp;
	char ch_num[20];
	static long old_ang = 0;
	static long old_pos = 0;
	static long old_angdot = 0;
	static long old_posdot = 0;

	settextstyle(TRIPLEX_FONT, HORIZ_DIR, 1);

	temp = angle * 179.999 / M_PI;
	if (old_ang != (long)(temp * 1000)){
		old_ang = (long)(temp * 1000);
		sprintf(ch_num, "%5.2f", fabs(temp));
		setcolor(BLUE);
		setfillstyle(SOLID_FILL, BLUE);
		bar(220, 60, 298, 80);
		setcolor(YELLOW);
		if (temp<0)
			outtextxy(220,56,"-");

		outtextxy(232, 56, ch_num);
	}

	temp = angleDot * 179.999 / M_PI;
	if (old_angdot != (long)(temp * 1000)){
		old_angdot = (long)(temp * 1000);
		sprintf(ch_num, "%5.2f", fabs(temp));
		setcolor(BLUE);
		setfillstyle(SOLID_FILL, BLUE);
		bar(470, 60, 555, 80);
		setcolor(YELLOW);
		if (temp<0)
			outtextxy(470,56,"-");

		outtextxy(482, 56, ch_num);
	}

	temp = pos * 999.999;
	if (old_pos != (long)(temp*1000)){
		old_pos = (long)(temp * 1000);
		sprintf(ch_num, "%5.2f", fabs(temp));
		setcolor(BLUE);
		setfillstyle(SOLID_FILL, BLUE);
		bar(220, 90, 298, 110);
		setcolor(YELLOW);
		if (temp<0)
			outtextxy(220,86,"-");

		outtextxy(232, 86, ch_num);
	}

	temp = posDot * 999.999;
	if (old_posdot != (long)(temp*1000)){
		old_posdot = (long)(temp * 1000);
		sprintf(ch_num, "%5.2f", fabs(temp));
		setcolor(BLUE);
		setfillstyle(SOLID_FILL, BLUE);
		bar(470, 90, 555, 110);
		setcolor(YELLOW);
		if (temp<0)
			outtextxy(470,86,"-");

		outtextxy(482, 86, ch_num);
	}
}

void draw_curve()
{
	int i, j, k;

	dp[point_index].point_the = BASE_Y - (int)((angle) * 180 / M_PI * zoom_the * 30);
	if ((dp[point_index].point_the > BASE_Y + 120) || (dp[point_index].point_the < BASE_Y - 120))
		dp[point_index].point_the = BASE_Y;

	dp[point_index].point_pos = BASE_Y - (int)(pos * zoom_pos * 30);
	if ((dp[point_index].point_pos > BASE_Y + 120) || (dp[point_index].point_pos < BASE_Y - 120))
		dp[point_index].point_pos = BASE_Y;

	dp[point_index].point_thedot = BASE_Y - (int)(angleDot * 180 / M_PI * zoom_thedot * 30);
	if ((dp[point_index].point_thedot > BASE_Y + 120) || (dp[point_index].point_thedot < BASE_Y - 120))
		dp[point_index].point_thedot = BASE_Y;

	dp[point_index].point_posdot = BASE_Y - (int)(posDot * zoom_posdot * 30);
	if ((dp[point_index].point_posdot > BASE_Y + 120) || (dp[point_index].point_posdot < BASE_Y - 120))
		dp[point_index].point_posdot = BASE_Y;

	my_put_pixel(BASE_X + point_index / (MAX_POINT / 400), dp[point_index].point_the, LIGHTRED);
	my_put_pixel(BASE_X + point_index / (MAX_POINT / 400), dp[point_index].point_thedot, LIGHTGREEN);
	my_put_pixel(BASE_X + point_index / (MAX_POINT / 400), dp[point_index].point_pos, YELLOW);
	my_put_pixel(BASE_X + point_index / (MAX_POINT / 400), dp[point_index].point_posdot, LIGHTBLUE);

	point_index ++;

	if (point_index >= MAX_POINT){

		setviewport(BASE_X, BASE_Y - 120, BASE_X + 400, BASE_Y + 120, 1);
		clearviewport();
		setviewport(0, 0, getmaxx(), getmaxy(), 1);

	setcolor(LIGHTGRAY);
	setlinestyle(DOTTED_LINE, 0, NORM_WIDTH);
	for (i=1; i<8; i++)
		line(BASE_X, BASE_Y-120+i*30, BASE_X+400, BASE_Y-120+i*30);

	setlinestyle(SOLID_LINE, 0, NORM_WIDTH);
	for (i=1; i<10; i++)
		line(BASE_X+i*40, BASE_Y+117, BASE_X+i*40, BASE_Y+120);

		point_index = 0;
	}
}


void draw_pendulum()
{

	unsigned draw_size;

	int left, top, right, bottom;

	putimage(old_left, old_top, image, COPY_PUT);

	free(image);

	if (sin(angle) > 0){
		left = BASE_X + 177 - int(POS_SCREEN * pos) - int(sin(angle) * SCREEN_LENGTH) - 10;
		right = BASE_X + 177 - int(POS_SCREEN * pos) + 10;
	}
	else{
		right = BASE_X + 177 - int(POS_SCREEN * pos) - int(sin(angle) * SCREEN_LENGTH) + 10;
		left = BASE_X + 177 - int(POS_SCREEN * pos) - 10;
	}

	if (cos(angle) > 0){
		top = BASE_Y - 5 - 10;
		bottom = BASE_Y -5 + cos(angle) * SCREEN_LENGTH + 10;
	}
	else{
		top = BASE_Y -5 + cos(angle) * SCREEN_LENGTH - 10;
		bottom = BASE_Y - 5 + 10;
	}

	draw_size = imagesize(left, top, right, bottom);
	if ((image = malloc(draw_size)) == NULL){
		printf("fail");
		exit(1);
	}

	getimage(left, top, right, bottom, image);

	old_left = left;
	old_top = top;
	old_right = right;
	old_bottom = bottom;

	setcolor(LIGHTGRAY);
	setfillstyle(SOLID_FILL, LIGHTGRAY);
	bar(BASE_X + 170 - POS_SCREEN * pos,
		BASE_Y - 15,
		BASE_X + 185 - POS_SCREEN * pos,
		BASE_Y + 3);

	setcolor(WHITE);
	setlinestyle(SOLID_LINE, 1, THICK_WIDTH);

	line(BASE_X + 177 - int(POS_SCREEN * pos),
		BASE_Y - 5,
		BASE_X + 177 - int(POS_SCREEN * pos) - int(sin(angle) * SCREEN_LENGTH),
		BASE_Y -5 + cos(angle) * SCREEN_LENGTH);

}

void draw_curve_panel()
{
	int i;

	setcolor(CYAN);
	setfillstyle(SOLID_FILL, CYAN);
	bar(120+2, 120+2, 639, 420-2);

	// for test begain
	setcolor(BLACK);
	setfillstyle(SOLID_FILL, BLACK);
	bar(BASE_X, BASE_Y-120, BASE_X+400+1, BASE_Y+120);

	setcolor(MAGENTA); 
	//for test end
	setlinestyle(SOLID_LINE, 0, THICK_WIDTH);
	rectangle(BASE_X-2, BASE_Y-120-2, BASE_X+400+3, BASE_Y+120+2);

	WriteHz(BASE_X+20, BASE_Y-145, 1, 1, 2, WHITE, "偏角      角速度      位移      速度");
	setcolor(LIGHTRED);
	line(BASE_X+60, BASE_Y-137, BASE_X+80, BASE_Y-137);
	setcolor(LIGHTGREEN);
	line(BASE_X+170, BASE_Y-137, BASE_X+190, BASE_Y-137);
	setcolor(YELLOW);
	line(BASE_X+260, BASE_Y-137, BASE_X+280, BASE_Y-137);
	setcolor(LIGHTBLUE);
	line(BASE_X+350, BASE_Y-137, BASE_X+370, BASE_Y-137);

	setcolor(WHITE);
	setlinestyle(SOLID_LINE, 0, NORM_WIDTH);
	WriteHz(BASE_X-80, BASE_Y-100, 1, 1, 3, WHITE, "单位:");

	WriteHz(BASE_X-30, BASE_Y-60, 1, 1, 0, WHITE, "度");

	WriteHz(BASE_X-30, BASE_Y-30, 1, 1, 0, WHITE, "度");
	line(BASE_X-30, BASE_Y-12, BASE_X-14, BASE_Y-12);
	WriteHz(BASE_X-30, BASE_Y-10, 1, 1, 1, WHITE, "秒");

	WriteHz(BASE_X-30, BASE_Y+20, 1, 1, 1, WHITE, "米");

	WriteHz(BASE_X-30, BASE_Y+50, 1, 1, 1, WHITE, "米");
	line(BASE_X-30, BASE_Y+68, BASE_X-14, BASE_Y+68);
	WriteHz(BASE_X-30, BASE_Y+70, 1, 1, 1, WHITE, "秒");

	outtextxy(BASE_X-80, BASE_Y+100, "240");
	WriteHz(BASE_X-35, BASE_Y+105, 1, 1, 0, WHITE, "毫秒");

	draw_zoom_unit(zoom_the, 0);
	draw_zoom_unit(zoom_thedot, 1);
	draw_zoom_unit(zoom_pos, 2);
	draw_zoom_unit(zoom_posdot, 3);

	setcolor(LIGHTGRAY);
	setlinestyle(DOTTED_LINE, 0, NORM_WIDTH);
	for (i=1; i<8; i++)
		line(BASE_X, BASE_Y-120+i*30, BASE_X+400, BASE_Y-120+i*30);

	setlinestyle(SOLID_LINE, 0, NORM_WIDTH);
	for (i=1; i<10; i++)
		line(BASE_X+i*40, BASE_Y+117, BASE_X+i*40, BASE_Y+120);

	setcolor(WHITE);
	char unit_coor[3];
	for (i=10; i>=0; i--){
		sprintf(unit_coor, "%d", i);
		outtextxy(BASE_X+(10-i)*40-3, BASE_Y+125, unit_coor);
	}

	for (i=4; i>=0; i--){
		sprintf(unit_coor, "%d", i);
		outtextxy(BASE_X+413, BASE_Y-i*30-12, unit_coor);
	}

	for (i=-1; i>=-4; i--){
		sprintf(unit_coor, "%d", i);
		outtextxy(BASE_X+405, BASE_Y-i*30-12, unit_coor);
	}

}

void draw_pendulum_panel()
{
	setcolor(BLACK);
	setfillstyle(SOLID_FILL, BLACK);
	bar(BASE_X-88, BASE_Y-150+2, BASE_X+429, BASE_Y+150-2);
	//chain

	setcolor(CYAN);
	setlinestyle(SOLID_LINE, 0, NORM_WIDTH);
	setfillstyle(SOLID_FILL, CYAN);
	pieslice(BASE_X+6, BASE_Y, 0, 360, 8);
	pieslice(BASE_X+350, BASE_Y, 0, 360, 8);
	line(BASE_X+4,BASE_Y-8,BASE_X+348, BASE_Y-8);
	line(BASE_X+4, BASE_Y+7, BASE_X+348, BASE_Y+7);

	// base
	setcolor(LIGHTGRAY);
	setfillstyle(SOLID_FILL, LIGHTGRAY);
	bar(BASE_X-6,BASE_Y+18,BASE_X+364,BASE_Y+23);
	bar(BASE_X+28,BASE_Y-6,BASE_X+22,BASE_Y+18);
	bar(BASE_X+334,BASE_Y-6,BASE_X+338,BASE_Y+18);
	bar(BASE_X+28,BASE_Y-4,BASE_X+338,BASE_Y);

	// gear
	bar(BASE_X+4,BASE_Y,BASE_X+8,BASE_Y+18);
	bar(BASE_X+348,BASE_Y,BASE_X+352,BASE_Y+18);

	int left, top, right, bottom;

	if (sin(angle) > 0){
		left = BASE_X + 177 - int(POS_SCREEN * pos) - int(sin(angle) * SCREEN_LENGTH) - 10;
		right = BASE_X + 177 - int(POS_SCREEN * pos) + 10;
	}
	else{
		right = BASE_X + 177 - int(POS_SCREEN * pos) - int(sin(angle) * SCREEN_LENGTH) + 10;
		left = BASE_X + 177 - int(POS_SCREEN * pos) - 10;
	}

	if (cos(angle) > 0){
		top = BASE_Y - 5 - 10;
		bottom = BASE_Y -5 + cos(angle) * SCREEN_LENGTH + 10;
	}
	else{
		top = BASE_Y - 5 + cos(angle) * SCREEN_LENGTH - 10;
		bottom = BASE_Y - 5 + 10;
	}

	unsigned draw_size = imagesize(left, top, right, bottom);
	if ((image = malloc(draw_size)) == NULL){
		printf("fail");
		exit(1);
	}

	getimage(left, top, right, bottom, image);

	old_left = left;
	old_top = top;
	old_right = right;
	old_bottom = bottom;

	setcolor(LIGHTGRAY);
	setfillstyle(SOLID_FILL, LIGHTGRAY);
	bar(BASE_X + 170 - POS_SCREEN * pos,
		BASE_Y - 15,
		BASE_X + 185 - POS_SCREEN * pos,
		BASE_Y + 3);

	setcolor(WHITE);
	setlinestyle(SOLID_LINE, 1, THICK_WIDTH);

	line(BASE_X + 177 - int(POS_SCREEN * pos),
		BASE_Y - 5,
		BASE_X + 177 - int(POS_SCREEN * pos) - int(sin(angle) * SCREEN_LENGTH),
		BASE_Y -5 + cos(angle) * SCREEN_LENGTH);

}

void draw_zoom_unit(float zoom, int index)
{
	char unit_num[6];
	int unit_digit;

	setcolor(CYAN);
	setlinestyle(SOLID_LINE, 0, NORM_WIDTH);
	setfillstyle(SOLID_FILL, CYAN);
	bar(BASE_X-80, BASE_Y-75+index*40, BASE_X-40, BASE_Y-35+index*40);
	setcolor(YELLOW);
	if (zoom > 1){
		sprintf(unit_num, "%d", (int)zoom);
		unit_digit = strlen(unit_num);
		outtextxy(BASE_X-80 + (unit_digit-1)*5, BASE_Y-73+index*40, "1");
		line(BASE_X-80, BASE_Y-53+index*40, BASE_X-80+unit_digit*10, BASE_Y-53+index*40);
		outtextxy(BASE_X-80, BASE_Y-57+index*40, unit_num);
	}
	else{
		sprintf(unit_num, "%d", (int)(1 / zoom));
		outtextxy(BASE_X-80, BASE_Y-65+index*40, unit_num);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -