昨天写了一个代码,使用C语言画三角形,既然写了那个,那就继续画一条直线,这个也很有意思。
C语言不调用库函数画一个三角形
直线的方程是
如下图所示
— — 斜率的范围
(-∞,+∞)
既然知道公式了,我们就直接上代码吧,之前有人评论说 putchar
函数可以修改下,这次我们就修改下。
还有,我这次就不用浮点型了,直接用int来做计算。
#include "stdio.h"typedef struct {int x,y;
} Vec;
/*y=kx+b*/
int judge(Vec p,int k,int b){int tmp = p.y - k*p.x -b;if(tmp == 0)return 1;return 0;
}int main(){Vec p;int k = 1;int b = 16;for(p.y = 50;p.y>= -50;p.y -=2,putchar('\n'))for(p.x = -50;p.x <= 50;p.x++)putchar(judge(p,k,b)?'@':'.');
}
用图片来更直观的来感受下输出
如果坐标系更大,就会更细腻
—— 程序输出
weiqifa@bsp-ubuntu1804:~/c$ gcc elsfk.c && ./a.out
....................................................................................@................
..................................................................................@..................
................................................................................@....................
..............................................................................@......................
............................................................................@........................
..........................................................................@..........................
........................................................................@............................
......................................................................@..............................
....................................................................@................................
..................................................................@..................................
................................................................@....................................
..............................................................@......................................
............................................................@........................................
..........................................................@..........................................
........................................................@............................................
......................................................@..............................................
....................................................@................................................
..................................................@..................................................
................................................@....................................................
..............................................@......................................................
............................................@........................................................
..........................................@..........................................................
........................................@............................................................
......................................@..............................................................
....................................@................................................................
..................................@..................................................................
................................@....................................................................
..............................@......................................................................
............................@........................................................................
..........................@..........................................................................
........................@............................................................................
......................@..............................................................................
....................@................................................................................
..................@..................................................................................
................@....................................................................................
..............@......................................................................................
............@........................................................................................
..........@..........................................................................................
........@............................................................................................
......@..............................................................................................
....@................................................................................................
..@..................................................................................................
@....................................................................................................
.....................................................................................................
.....................................................................................................
.....................................................................................................
.....................................................................................................
.....................................................................................................
.....................................................................................................
.....................................................................................................
.....................................................................................................
推荐阅读:
专辑|Linux文章汇总
专辑|程序人生
专辑|C语言
我的知识小密圈
关注公众号,后台回复「1024」获取学习资料网盘链接。
欢迎点赞,关注,转发,在看,您的每一次鼓励,我都将铭记于心~
嵌入式Linux
微信扫描二维码,关注我的公众号