登录  | 立即注册

游客您好!登录后享受更多精彩

扫一扫,访问微社区

QQ登录

只需一步,快速开始

开启左侧

[寒假笔记] C语言学习

[复制链接]
发表于 2021-2-9 21:33:19 | 显示全部楼层 |阅读模式
学习笔记
学习科目: C语言
学习安排: C语言相关知识学习
开始时间: 2021-02-09
结束时间: 2021-02-09
画圣诞树
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>

  4. #define PI 3.14159265359

  5. float sx, sy;

  6. float sdCircle(float px, float py, float r)
  7. {
  8.         float dx = px-sx, dy = py-sy;
  9.         return sqrtf(dx*dx+dy*dy)-r;
  10. }

  11. float opUnion(float d1,float d2)
  12. {
  13.         return (d1<d2?d1:d2);
  14. }

  15. #define T px+scale*r*cosf(theta),py+scale*r*sin(theta)

  16. float f(float px,float py,float theta,float scale,int n)
  17. {
  18.         float d=0.0f;
  19.         for(float r=0.0f;r<0.8f;r+=0.02f)
  20.         {
  21.                 d=opUnion(d, sdCircle(T,0.05f*scale*(0.95f-r)));
  22.         }
  23.        
  24.         if(n>0)
  25.         {
  26.                 for(int t=-1;t<=1;t+=2)
  27.                 {
  28.                         float tt=theta+t*1.8f;
  29.                         float ss=scale*0.9f;
  30.                         for(float r=0.2f;r<0.8f;r+=0.1f)
  31.                         {
  32.                                 d=opUnion(d,f(T,tt,ss*0.5f,n-1));
  33.                                 ss*=0.8f;
  34.                         }
  35.                 }
  36.         }
  37.         return d;
  38. }

  39. int main(int argc,char*argv[])
  40. {
  41.         int n=argc>1?atoi(argv[1]):3;
  42.         for(sy=0.8f;sy>0.0f;sy-=0.02f,putchar('\n'))
  43.         {
  44.                 for(sx=-0.35f;sx<0.35f;sx+=0.01f)
  45.                 {
  46.                         putchar(f(0,0,PI*0.5f,1.0f,n)<0?'*':' ');
  47.                 }
  48.         }
  49. }
复制代码
结果 1.png
好懒~~不想说~~~
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表