Skip to content

Instantly share code, notes, and snippets.

@riicchhaarrd
Last active September 14, 2019 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riicchhaarrd/a40c5bfceac76cab58f24093933fc935 to your computer and use it in GitHub Desktop.
Save riicchhaarrd/a40c5bfceac76cab58f24093933fc935 to your computer and use it in GitHub Desktop.
drawing a shaded sphere (using https://cidscropt.org/)
dot(a,b)
{
return (a.x*b.x+a.y*b.y+a.z*b.z);
}
_sqrt(a)
{
prev = 0;
for(i=0.0;i<10000;i = i + 0.1)
{
if(i*i>a)
return prev;
prev = i;
}
return 0;
}
len(v)
{
return _sqrt(dot(v,v));
}
dot_n(a,b)
{
return dot(a,b) / (len(a) * len(b));
}
clamp(v)
{
x = v.x;
y = v.y;
z = v.z;
if(x > 1)
{
x = 1;
}
if(y > 1)
{
y = 1;
}
if(z > 1)
{
z = 1;
}
return (x,y,z);
}
draw_circle_filled2(at,start,end,color)
{
targ=(at+start/4);
tlen=len(targ);
end2=end*end;
for(x = start;x<end;x++)
{
for(y = start;y<end;y++)
{
if(x*x+y*y<=end2)
{
pos = (at.x+x,at.y+y,0);
d = pos - targ;
s = len(d) / tlen;
s *= 3;
v = ((1,1,1) - clamp((s,s,s))) * 255;
set_pixel(pos,v);
}
}
}
}
main()
{
printf("hello graphics\n");
draw_circle_filled2((150,150,0),-50,50,(255,0,0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment