当前位置:首页 » 操作系统 » c画图源码

c画图源码

发布时间: 2022-09-14 06:14:15

① 求一用c语言画直线的程序

不调用画图 API,用C 或 C++ 如何实现画一条线?
Milo Yip:如何开始用 C++ 写一个光栅化渲染器?
我尝试用不同技术实现画直线的方法(完整源代码在 miloyip/line),此文简单介绍个中思路。本文的代码采用 C 语言、标准库及极简的 PNG 编码函数 svpng(),没有使用其他 API。

1. Bresenham 算法
Bresenham直线算法 [1] 是最简单的直线光栅化(rasterization)算法。


Bresenham 直线
如果像上图,直线的高度小于宽度,那么 Bresenham 直线算法会为 轴每个坐标填入一个像素,绘画每个像素时按斜率判断 是否需要调整。整个算法可以避开浮点数运算,只用整数运算实现。以下是一个简单实现:

// Modified from https://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#C
void bresenham(int x0, int y0, int x1, int y1) {
int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
int dy = abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
int err = (dx > dy ? dx : -dy) / 2;

while (setpixel(x0, y0), x0 != x1 || y0 != y1) {
int e2 = err;
if (e2 > -dx) { err -= dy; x0 += sx; }
if (e2 < dy) { err += dx; y0 += sy; }
}
}
为了测试不同角度,我做了一个测试用例:

int main() {
memset(img, 255, sizeof(img));
float cx = w * 0.5f - 0.5f, cy = h * 0.5f - 0.5f;
for (int j = 0; j < 5; j++) {
float r1 = fminf(W, H) * (j + 0.5f) * 0.085f;
float r2 = fminf(W, H) * (j + 1.5f) * 0.085f;
float t = j * PI / 64.0f;
for (int i = 1; i <= 64; i++, t += 2.0f * PI / 64.0f) {
float ct = cosf(t), st = sinf(t);
bresenham((int)(cx + r1 * ct), (int)(cy - r1 * st), (int)(cx + r2 * ct), (int)(cy - r2 * st));
}
}
svpng(fopen("line_bresenham.png", "wb"), W, H, img, 0);
}
完整代码 line_bresenham.c

渲染结果及中间局部放大:



2. 采样方法
Bresenham 直线算法有 3 个问题:

不能控制直线宽度;
坐标为整数;
只能对像素写入一个颜色,只用单色会有严重的锯齿效果。
在图形学中,除了以逐个图元(如直线)方式渲染,我们还可以通过对每个像素进行采样(sampling),换句话说,我们可对整个图像逐像素询问:“这个像素的颜色是什么?”

用采样方式画直线时,我们可以用一个具有面积的形状去表示“直线”,例如是长方形。但在本文中,我们使用胶囊体(capsule)去表示直线。这样就能解决上面前两个问题:(1) 可用胶囊体半径设置直线的宽度;(2) 坐标可以为浮点数。不过,用最简单的采样方式,我们需要在每像素查询所有图元是否占有该像素,效率很低。

② 用c语言编写一个画图工具 哪位大侠会呀 编一个最简单的 能给个简单一点的画图工具的源代码么 跪谢啦

自己写吧,不然怎么进步?随便找个MFC的书,里面都可以找到画图相关的内容,定义一个对话框,然后在上面随便定义个画笔,刷点东西。做到这一步你就知道接下来怎么做了

③ 谁会用c语言设计数学函数绘图 可画一次和二次函数图像 的源代码

你用cad软件做这样的图太简单了

④ 求一个纯C语言绘图函数

SDL可以做到。给你贴个源码

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<SDL/SDL.h>
#include<SDL/SDL_image.h>

//函数声明
int Init(void);
SDL_Surface *createScreen(int width,int height,int bpp,Uint32 flags);
void destroyScreen(SDL_Surface *screen);

SDL_Surface *loadImageBMP(char *filename);
void loadImage(char *filename,SDL_Surface *screen,int xPos,int yPos);

SDL_Surface *screen;
SDL_Surface *image_handler;
char buffer[10];

int main(int argc,char *argv[])
{
int height=640,width=480;
int bpp=8;

Init();
screen = createScreen(width,height,bpp,SDL_SWSURFACE);
loadImage("./wuyajie.bmp",screen,width/2,height/4);
read(STDIN_FILENO,buffer,1);
destroyScreen(screen);
SDL_Quit();
return 0;
}

int Init(void)
{
if(SDL_Init(SDL_INIT_VIDEO)==-1)
{
fprintf(stderr,"SDL Init Error:%s\n",SDL_GetError());
exit(-1);
}
return 0;
}

/*
创建屏幕,并将屏幕设置为640x480大小
*/
SDL_Surface *createScreen(int width,int height,int bpp,Uint32 flags)
{
SDL_Surface *screen;
if((screen = SDL_SetVideoMode(width,height,bpp,flags))==NULL)
{
fprintf(stderr,"Could not create a screen:%s\n",SDL_GetError());
exit(-1);
}
return screen;
}

/*
加载图片
*/
void loadImage(char *filename,SDL_Surface *screen,int xPos,int yPos)
{
SDL_Surface *image;
SDL_Rect dest;

image = SDL_LoadBMP(filename);

if ( image == NULL ){
fprintf(stderr, "无法加载 %s: %s\n", filename, SDL_GetError());
exit(-1);
}

dest.x = xPos;
dest.y = yPos;

dest.w = image->w;
dest.h = image->h;
SDL_BlitSurface(image,NULL,screen,&dest);
SDL_UpdateRects(screen,1,&dest);

}

void destroyScreen(SDL_Surface * screen)
{
SDL_FreeSurface(screen);
}

⑤ 求一段画圆的程序源代码,要求是C 或者C++ 的语言

以x=0,y=0为圆心,r为半径,屏幕左上角为坐标原点
for (x=-r;x<r;x++)
{
//计算y
y=sqrt(r*r-x*x);
//画点
setpix(x+r, y+r);
setpix(x+r, 0-y+r);
}
老兄,setpix()不是画图函数,而是画点函数,不画点怎么画图啊

⑥ 跪求关于c程序源代码,运行后的效果为动态图形,比如圆啊,三角形之类的,最好附上代码的解释

这个一个VC中画圆的代码(可能你还需要配置编译环境):
#include <GL/glut.h> // 设置头文件
#include <stdlib.h>
#include <math.h>

/*void setPixel ( int x, int y)
{
glBegin ( GL_POINTS);
glVertex2i (x, y);
glEnd ();
}

inline int round (const float a ) { return int (a + 0.5); }

void lineDDA (int x0, int y0, int xEnd, int yEnd)
{
int dx = xEnd - x0, dy = yEnd - y0, steps, k;
float xIncrement, yIncrement, x = x0, y = y0;

if (fabs (dx) > fabs (dy))
steps = fabs (dx);
else
steps = fabs (dy);
xIncrement = float (dx) / float (steps);
yIncrement = float (dy) / float (steps);

setPixel (round (x), round (y));
for (k = 0; k < steps; k++) {
x += xIncrement;
y += yIncrement;
setPixel (round (x), round (y));
}
} */

class screenPt
{
private:
GLint x,y;
public:
screenPt()
{
x=y=0;
}
void setCoords (GLint xCoordValue,GLint yCoordValue)
{
x=xCoordValue;
y=yCoordValue;
}
GLint getx() const{
return x;
}
GLint gety() const{
return y;
}
void incrementx()
{
x++;
}
void decrementy()
{
y--;
}
};

void setPixel(GLint xCoord,GLint yCoord)
{
glBegin (GL_POINTS);
glVertex2i(xCoord,yCoord);
glEnd();
}
void circleMidpoint(GLint xc,GLint yc,GLint radius)
{
screenPt circPt;
GLint p=1-radius; //决策参数
circPt.setCoords(0,radius);
void circlePlotPoints (GLint ,GLint,screenPt);
circlePlotPoints(xc,yc,circPt);
while(circPt.getx()<circPt.gety())
{
circPt.incrementx();
if(p<0)
p += 2*circPt.getx() + 1;
else
{
circPt.decrementy();
p += 2*(circPt.getx() - circPt.gety()) + 1;
}
circlePlotPoints(xc,yc,circPt);
}
}

void circlePlotPoints(GLint xc,GLint yc,screenPt circPt)
{
setPixel (xc + circPt.getx(),yc + circPt.gety());
setPixel (xc - circPt.getx(),yc + circPt.gety());
setPixel (xc + circPt.getx(),yc - circPt.gety());
setPixel (xc - circPt.getx(),yc - circPt.gety());
setPixel (xc + circPt.gety(),yc + circPt.getx());
setPixel (xc - circPt.gety(),yc + circPt.getx());
setPixel (xc + circPt.gety(),yc - circPt.getx());
setPixel (xc - circPt.gety(),yc - circPt.getx());
}
void init (void) // 初始化函数
{
glClearColor (1.0, 1.0, 1.0, 0.0); // 设置清屏的颜色(Red,green,blue),但不能让该颜色在显示窗上
// 出现
glMatrixMode (GL_PROJECTION); // 设置投影矩阵
glLoadIdentity (); // 单位化上述投影矩阵
gluOrtho2D (0.0, 800.0, 0.0, 600.0); // 设置具体投影 矩阵为平面正交投影 4/3
}
void lineSegment (void) //绘图函数
{
glClear (GL_COLOR_BUFFER_BIT); // 按glClearColor (1.0, 1.0, 1.0, 0.0);指定的颜色刷新颜色
glColor3f (1.0, 0.0, 0.0); //设置前景色,即画图的颜色
circleMidpoint(400,300,200);
glFlush ( ); // 在每一画面或场景的结尾处调用,强制前面发出的OpenGL 命令开始执行

}
void main (int argc, char** argv) // OpenGL 绘图主函数
{
glutInit (&argc, argv); // 初始化GLUT 库
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // 设置 显示模式
glutInitWindowPosition (50, 100); // 窗口左上角像素坐标
glutInitWindowSize (800, 600); // 设置设备显示窗口大小 4/3
glutCreateWindow ("An Example OpenGL Program"); // 创建一个窗口,参数为窗口的标题

init ( ); // 函数调用.
glutDisplayFunc (lineSegment); // 绘制当前窗口
glutMainLoop ( ); //通常用于程序的结尾,表示开始运行程序.显示出所有创建的窗口

}

⑦ ubuntu系统如何用c语言绘图

可以使用 plplot 函数库,需要自己安装,安装后在源代码中 include plplot.h 就能用了

⑧ 分形彩图的C或者matlab的源代码(至少3段)

#include<graphics.h>
#include<stdlib.h>
#include<math.h>

int main()
{
float m,dx,dy,x,y,x_n,y_n,Cx,Cy;
int n,i,j,L=4;

int gdriver=DETECT,gmode; //gdriver和gmode分别表示图形驱动器和模式,gdriver=DETECT是在测试显示器硬件

initgraph(&gdriver,&gmode,""); //初始化图形模式
setbkcolor(1); //设置背景色为蓝色
dx=3.0/639;
dy=2.2/479;
for(i=0;i<639;i++)
{
Cx=-1.9+i*dx;
for(j=0;j<479;j++)
{
Cy=-1.2+j*dy;
x=y=0;
for(n=0;n<=1000;n++)
{
x_n=x*x-y*y+Cx;
y_n=2*x*y+Cy;
m=x_n*x_n;
if(m>L) break;
x=x_n;
y=y_n;
}
putpixel(i,j,(int)(0.4*m)%16); //在指定位置画一像素(坐标(i,j),第三个计算式为画点的颜色)
}
}
getch(); //任意键返回
closegraph(); //关闭图形模式并返回文本模式
}

关于颜色的设置如下:
━━━━━━━━━━━━━━━━━━━━━━━━━━
符号常数 数值 含义 字符或背景
——————————————————————————
BLACK 0 黑 两者均可
BLUE 1 兰 两者均可
GREEN 2 绿 两者均可
CYAN 3 青 两者均可
RED 4 红 两者均可
MAGENTA 5 洋红 两者均可
BROWN 6 棕 两者均可
LIGHTGRAY 7 淡灰 两者均可
DARKGRAY 8 深灰 只用于字符
LIGHTBLUE 9 淡兰 只用于字符
LIGHTGREEN 10 淡绿 只用于字符
LIGHTCYAN 11 淡青 只用于字符
LIGHTRED 12 淡红 只用于字符
LIGHTMAGENTA 13 淡洋红 只用于字符
YELLOW 14 黄 只用于字符
WHITE 15 白 只用于字符
BLINK 128 闪烁 只用于字符

⑨ 求C源码:计算机图形学程序——扫描线算法

这是我以前上学的时候写的,你改改,凑活用巴。

=============================

#include <graphics.h>
#include <stdio.h>
#include <dos.h>
#include <math.h>
#include <bios.h>
#include <string.h>

void parspl ( int p[10000][2] , long n , int precision , int color )
{
int x = 0 , y = 0 , i = 0 , j = 0 , m = 0 ;
double t2 = 0 , t3 = 0 , t = 0 , a , b , c , d , e = p[1][0] + 5 , f = p[1][1] + 5 ;
setcolor ( color ) ;
m=n+1;
p[0][0] = p[n][0] ;
p[0][1] = p[n][1] ;
p[m][0] = p[1][0] ;
p[m][1] = p[1][1] ;
p[m+1][0] = p[2][0] ;
p[m+1][1] = p[2][1] ;
moveto ( p[1][0] , p[1][1] ) ;
for ( i = 0 ; i < n ; i ++ )
{
t = 0.5 / precision ;
for ( j = 1 ; j < precision ; j ++ )
{
t2 = t * t ;
t3 = t2 * t ;
a = 4 * t2 - t - 4 * t3 ;
b = 1 - 10 * t2 + 12 * t3 ;
c = t + 8 * t2 - 12 * t3 ;
d = 4 * t3 - 2 * t2 ;
x = a * p[i][0] + b * p[i+1][0] + c * p[i+2][0] + d * p[i+3][0] ;
y = a * p[i][1] + b * p[i+1][1] + c * p[i+2][1] + d * p[i+3][1] ;
lineto ( x , y ) ;
line ( e , f , x + 5 , y + 5 ) ;
moveto ( x , y ) ;
t += 0.5 / precision ;
e = x + 5 ;
f = y + 5 ;
}
lineto ( p[i+2][0] , p[i+2][1] ) ;
moveto ( e , f ) ;
lineto ( p[i+2][0] + 5 , p[i+2][1] + 5 ) ;
moveto ( p[i+2][0] , p[i+2][1] ) ;
}
}

int main()
{
long n = 5 ;
char pwd[4] ;
int p[100][2] ;
int a1 , a2 , b1 , b2 , c1 , c2 , d1 , d2 , e1 , e2 ;
int gdriver = VGA , gmode = VGAHI ;
initgraph( &gdriver , &gmode , "c:\\tc" ) ;
setbkcolor ( 0 ) ;
a1 = p[1][0] = 320 ;
a2 = p[1][1] = 240 ;
b1 = p[2][0] = 320 ;
b2 = p[2][1] = 120 ;
c1 = p[3][0] = 452 ;
c2 = p[3][1] = 128 ;
d1 = p[4][0] = 382 ;
d2 = p[4][1] = 388 ;
e1 = p[5][0] = 364 ;
e2 = p[5][1] = 280 ;
loop:
if ( a1 <= p[1][0] && p[1][0] <= 520 )
{
a1 = p[1][0] ;
p[1][0] += 1 ;
}
else
{
if ( p[1][0] >= 120 )
{
a1 = p[1][0] ;
p[1][0] -= 1 ;
}
else
a1 = p[1][0] = 120 ;
}
if ( a2 >= p[1][1] && p[1][1] >= 60 )
{
a2 = p[1][1] ;
p[1][1] -= 2 ;
}
else
{
if ( p[1][1] <= 420 )
{
a2 = p[1][1] ;
p[1][1] += 2 ;
}
else
a2 = p[1][1] = 420 ;
}
if ( b1 >= p[2][0] && p[2][0] >= 120 )
{
b1 = p[2][0] ;
p[2][0] -= 2 ;
}
else
{
if ( p[2][0] <= 520 )
{
b1 = p[2][0] ;
p[2][0] += 2 ;
}
else
b1 = p[2][0] = 520 ;
}
if ( b2 <= p[2][1] && p[2][1] <= 420 )
{
b2 = p[2][1] ;
p[2][1] += 1 ;
}
else
{
if ( p[2][1] >= 60 )
{
b2 = p[2][1] ;
p[2][1] -= 1 ;
}
else
b2 = p[2][1] = 60 ;
}
if ( c1 <= p[3][0] && p[3][0] <= 520 )
{
c1 = p[3][0] ;
p[3][0] += 1 ;
}
else
{
if ( p[3][0] >= 120 )
{
c1 = p[3][0] ;
p[3][0] -= 1 ;
}
else
c1 = p[3][0] = 120 ;
}
if ( c2 <= p[3][1] && p[3][1] <= 420 )
{
c2 = p[3][1] ;
p[3][1] += 1 ;
}
else
{
if ( p[3][1] >= 60 )
{
c2 = p[3][1] ;
p[3][1] -= 1 ;
}
else
c2 = p[3][1] = 60 ;
}
if ( d1 >= p[4][0] && p[4][0] >= 120 )
{
d1 = p[4][0] ;
p[4][0] -= 1 ;
}
else
{
if ( p[4][0] <= 520 )
{
d1 = p[4][0] ;
p[4][0] += 1 ;
}
else
d1 = p[4][0] = 520 ;
}
if ( d2 >= p[4][1] && p[4][1] >= 60 )
{
d2 = p[4][1] ;
p[4][1] -= 1 ;
}
else
{
if ( p[4][1] <= 420 )
{
d2 = p[4][1] ;
p[4][1] += 1 ;
}
else
d2 = p[4][1] = 420 ;
}
if ( e1 <= p[5][0] && p[5][0] <= 520 )
{
e1 = p[5][0] ;
p[5][0] += 1 ;
}
else
{
if ( p[5][0] >= 120 )
{
e1 = p[5][0] ;
p[5][0] -= 1 ;
}
else
e1 = p[5][0] = 120 ;
}
if ( e2 <= p[5][1] && p[5][1] <= 420 )
{
e2 = p[5][1] ;
p[5][1] += 2 ;
}
else
{
if ( p[5][1] >= 60 )
{
e2 = p[5][1] ;
p[5][1] -= 2 ;
}
else
e2 = p[5][1] = 60 ;
}
parspl ( p , n , 100 , 4 ) ;
parspl ( p , n , 100 , 4 ) ;
if ( bioskey ( 1 ) > 0 )
{
printf ("Please Input Password:") ;
gets (pwd) ;
if ( !strcmp(pwd,"cmy") )
{
clearviewport () ;
closegraph () ;
return 1 ;
}
}
cleardevice () ;
goto loop ;
}

⑩ 哦求c语言图形编程源代码O(∩_∩)O3

//program1
#include<stdio.h>
intmain()
{
inti,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=4-i;j++)
printf("");
for(j=1;j<=i*2-1;j++)
printf("%d",i);
}
for(i=3;i>=1;i--)
{
for(j=1;j<=4-i;j++)
printf("");
for(j=1;j<=i*2-1;j++)
printf("%d",i);
}
return0;
}
//program2
#include<stdio.h>
intmain()
{
inti,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=4-i;j++)
printf("");
for(j=1;j<=i*2-1;j++)
printf("%d",5-i);
}
for(i=3;i>=1;i--)
{
for(j=1;j<=4-i;j++)
printf("");
for(j=1;j<=i*2-1;j++)
printf("%d",5-i);
}
return0;
}

热点内容
手机怎么配置内存大 发布:2025-05-16 15:18:06 浏览:166
xpshop源码 发布:2025-05-16 15:17:25 浏览:404
android弹出通知 发布:2025-05-16 14:59:20 浏览:511
数据库EST 发布:2025-05-16 14:59:15 浏览:198
android版本号修改 发布:2025-05-16 14:53:48 浏览:174
android相机闪光灯 发布:2025-05-16 14:35:49 浏览:260
服务器无法通过ip访问 发布:2025-05-16 14:26:13 浏览:541
网吧u盘拒绝访问 发布:2025-05-16 14:13:50 浏览:261
无线网检查网络配置是怎么回事 发布:2025-05-16 14:04:03 浏览:222
网络爬虫python代码 发布:2025-05-16 14:03:26 浏览:517