昨天写了一个代码,使用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)?'@':'.');
}
用图片来更直观的来感受下输出
![01d1b931b9f9949e1349bcf3878258e5.png](https://i-blog.csdnimg.cn/blog_migrate/67bc6fbac3677f1b24fac3652dde524e.png)
如果坐标系更大,就会更细腻
![b4f3af26d1e7d41c0ed19042dfea0937.png](https://i-blog.csdnimg.cn/blog_migrate/011b59341527857e8c5b3e9cb31f7a3a.png)
—— 程序输出
weiqifa@bsp-ubuntu1804:~/c$ gcc elsfk.c && ./a.out
....................................................................................@................
..................................................................................@..................
................................................................................@....................
..............................................................................@......................
............................................................................@........................
..........................................................................@..........................
........................................................................@............................
......................................................................@..............................
....................................................................@................................
..................................................................@..................................
................................................................@....................................
..............................................................@......................................
............................................................@........................................
..........................................................@..........................................
........................................................@............................................
......................................................@..............................................
....................................................@................................................
..................................................@..................................................
................................................@....................................................
..............................................@......................................................
............................................@........................................................
..........................................@..........................................................
........................................@............................................................
......................................@..............................................................
....................................@................................................................
..................................@..................................................................
................................@....................................................................
..............................@......................................................................
............................@........................................................................
..........................@..........................................................................
........................@............................................................................
......................@..............................................................................
....................@................................................................................
..................@..................................................................................
................@....................................................................................
..............@......................................................................................
............@........................................................................................
..........@..........................................................................................
........@............................................................................................
......@..............................................................................................
....@................................................................................................
..@..................................................................................................
@....................................................................................................
.....................................................................................................
.....................................................................................................
.....................................................................................................
.....................................................................................................
.....................................................................................................
.....................................................................................................
.....................................................................................................
.....................................................................................................