Sheep (Ovis aries) are quadrupedal, ruminant mammals typically kept as livestock.
Log in to post a comment.
// Sheep. Created by Reinder Nijhoff 2020
// @reindernijhoff
//
// https://turtletoy.net/turtle/dd73eeec09
//
const grid = 5; // min=1, max=20, step=1
const changeWhite = 1; // min=0, max=1, step=0.01
const changeFlip = 0.05; // min=0, max=1, step=0.01
const turtle = new Turtle();
const polygons = new Polygons();
const tra = (p, t) => [p[0]+t[0], p[1]+t[1]];
const rot = (p, a) => [p[0]*Math.cos(a)+p[1]*Math.sin(a), p[1]*Math.cos(a)-p[0]*Math.sin(a)];
const scl = (p, sx, sy) => [p[0]*sx, p[1]*(sy?sy:sx)];
const fly = p => [p[0], -p[1]];
const flx = p => [-p[0], p[1]];
const I = p => p;
const ran = (a, s = .2) => a - s + 2 * s * Math.random();
const rn2 = (a, s = [.2,.2]) => [ran(a[0],s[0]), ran(a[1], s[1])];
function walk(i) {
const x = i % grid;
const y = (i/grid)|0;
const flip = (Math.random() < changeFlip ? -1 : 1);
sheep(turtle, p => tra(scl(p, 5/grid * flip, 5/grid), [(x+.5)*190/grid-95, -(y+.5)*190/grid+95]));
return i < grid*grid-1;
}
function cloud(turtle, tr, addHatching, segments = 9, detail = .32) {
const t = p => tr([p[0], .75*p[1]]);
const points = [], s=Math.random();
for (let i=0; i<segments; i++) {
const angle = (i+s+Math.random()*.25) * Math.PI * 2 / segments;
const radius = 9+1.5*Math.random();
points[i] = scale2([Math.cos(angle), Math.sin(angle)], radius);
}
const poly = polygons.create();
for (let i=0; i<segments; i++) {
const sp = points[i], ep = points[(i+1) % segments];
const sc = scale2(add2(scale2(sp, 3), scale2(ep, 1)), detail);
const ec = scale2(add2(scale2(sp, 1), scale2(ep, 3)), detail);
addBezier(poly, t(sp), t(sc), t(ec), t(ep), true, true);
}
if (addHatching) poly.addHatching(-Math.PI/4, .5);
polygons.draw(turtle, poly);
}
function arc(turtle, tr, addHatching) {
const p0 = rn2([3,3]), p1 = rn2([-9, 1]), p2 = rn2([1,-5]);
const p0c = add2(p0, rn2([-2,3])), p1c0 = add2(p1, rn2([0,4]));
const p2c = add2(p2, rn2([-2,0])), p1c1 = add2(p1, rn2([0,-4.5]));
const poly = polygons.create();
addBezier(poly, tr(p0), tr(p0c), tr(p1c0), tr(p1), true, true);
addBezier(poly, tr(p1), tr(p1c1), tr(p2c), tr(p2), true, true);
addPoints(poly, [tr([6,-2]), tr([6,0])], true, false);
if (addHatching) poly.addHatching(-Math.PI/4, .5);
polygons.draw(turtle, poly);
}
function head(turtle, tr, addHatching) {
const angleHead = ran(0.4, .35), angleEar = ran(3.5, .3);
const offsetHead = rn2([-11, -1.5]);
const t = p => tr(tra(scl(rot(p, angleHead), .75), offsetHead));
// eye
const poly = polygons.create();
addArc(poly, t([0,-2]), len2(sub2(t([0,-2]),t([0,-2.25]))), 0, Math.PI*2, 10, true, true);
polygons.draw(turtle, poly);
// ear
arc(turtle, p => t(tra(scl(rot(fly(p), angleEar), .35), [6, -2])), addHatching);
// head
arc(turtle, t, addHatching);
// hair
cloud(turtle, p => t(tra(scl(rot(p, -.4), .35), [4, -4])), addHatching, 6, 2/5);
}
function leg(turtle, tr, addHatching) {
const p0 = [-.4,0], p1 = [-.5,8], p2 = [1,8], p3 = [0,0];
const puc = [-1,3], pdc = [-1,-1], p1c = [.5,.5], p2c = [-.5,.5];
const poly = polygons.create();
addBezier(poly, tr(p0), tr(add2(p0,puc)), tr(add2(p1,pdc)), tr(p1), true, true);
addBezier(poly, tr(p1), tr(add2(p1,p1c)), tr(add2(p2,p2c)), tr(p2), true, true);
addBezier(poly, tr(p2), tr(add2(p2,pdc)), tr(add2(p3,puc)), tr(p3), true, true);
if (addHatching) poly.addHatching(-Math.PI/4, .5);
polygons.draw(turtle, poly);
}
function sheep(turtle, tr) {
let t = tr;
const addHatching = Math.random() > changeWhite;
head(turtle, t, addHatching);
cloud(turtle, t, addHatching);
const f = -.2, b = -0.1, a = .15+.3*Math.random();
leg(turtle, p => t(tra(rot(p,f+a),[-4,3.25])), !addHatching);
leg(turtle, p => t(tra(rot(p,f-a),[-4,3.6])), !addHatching);
leg(turtle, p => t(tra(rot(p,b-a),[ 7,3.25])), !addHatching);
leg(turtle, p => t(tra(rot(p,b+a),[ 7,3.25])), !addHatching);
}
function bezier(p0, p1, p2, p3, t) {
const k = 1 - t;
return [ k*k*k*p0[0] + 3*k*k*t*p1[0] + 3*k*t*t*p2[0] + t*t*t*p3[0],
k*k*k*p0[1] + 3*k*k*t*p1[1] + 3*k*t*t*p2[1] + t*t*t*p3[1] ];
}
function addPoints(poly, points, asEdge, asLine) {
if (asEdge) poly.addPoints(...points);
if (asLine) {
for (let i=0, steps=10; i<steps; i++) {
poly.addSegments(points[i], points[i+1]);
}
}
}
function addBezier(poly, sp, sc, ec, ep, asEdge, asLine) {
const points = [];
for (let i=0, steps=10; i<=steps; i++) {
points.push(bezier(sp, sc, ec, ep, i/steps));
}
addPoints(poly, points, asEdge, asLine);
}
function addArc(poly, center, radius, sa, ea, steps, asEdge, asLine) {
const points = [];
for (let i=0; i<=steps; i++) {
const a = sa + i*(ea-sa)/steps;
points.push(add2(center, [radius*Math.cos(a), radius*Math.sin(a)]));
}
addPoints(poly, points, asEdge, asLine);
}
function scale2(a,b) { return [a[0]*b,a[1]*b]; }
function add2(a,b) { return [a[0]+b[0],a[1]+b[1]]; }
function sub2(a,b) { return [a[0]-b[0],a[1]-b[1]]; }
function len2(a) { return Math.sqrt(a[0]**2 + a[1]**2); }
////////////////////////////////////////////////////////////////
// Polygon Clipping utility code - Created by Reinder Nijhoff 2019
// https://turtletoy.net/turtle/a5befa1f8d
////////////////////////////////////////////////////////////////
function Polygons() {
const polygonList = [];
const Polygon = class {
constructor() {
this.cp = []; // clip path: array of [x,y] pairs
this.dp = []; // 2d lines [x0,y0],[x1,y1] to draw
this.aabb = []; // AABB bounding box
}
addPoints(...points) {
// add point to clip path and update bounding box
let xmin = 1e5, xmax = -1e5, ymin = 1e5, ymax = -1e5;
(this.cp = [...this.cp, ...points]).forEach( p => {
xmin = Math.min(xmin, p[0]), xmax = Math.max(xmax, p[0]);
ymin = Math.min(ymin, p[1]), ymax = Math.max(ymax, p[1]);
});
this.aabb = [(xmin+xmax)/2, (ymin+ymax)/2, (xmax-xmin)/2, (ymax-ymin)/2];
}
addSegments(...points) {
// add segments (each a pair of points)
points.forEach(p => this.dp.push(p));
}
addOutline() {
for (let i = 0, l = this.cp.length; i < l; i++) {
this.dp.push(this.cp[i], this.cp[(i + 1) % l]);
}
}
draw(t) {
for (let i = 0, l = this.dp.length; i < l; i+=2) {
t.jump(this.dp[i]), t.goto(this.dp[i + 1]);
}
}
addHatching(a, d) {
const tp = new Polygon();
tp.cp.push([-1e5,-1e5],[1e5,-1e5],[1e5,1e5],[-1e5,1e5]);
const dx = Math.sin(a) * d, dy = Math.cos(a) * d;
const cx = Math.sin(a) * 200, cy = Math.cos(a) * 200;
for (let i = 0.5; i < 150 / d; i++) {
tp.dp.push([dx * i + cy, dy * i - cx], [dx * i - cy, dy * i + cx]);
tp.dp.push([-dx * i + cy, -dy * i - cx], [-dx * i - cy, -dy * i + cx]);
}
tp.boolean(this, false);
this.dp = [...this.dp, ...tp.dp];
}
inside(p) {
let int = 0; // find number of i ntersection points from p to far away
for (let i = 0, l = this.cp.length; i < l; i++) {
if (this.segment_intersect(p, [0.1, -1000], this.cp[i], this.cp[(i + 1) % l])) {
int++;
}
}
return int & 1; // if even your outside
}
boolean(p, diff = true) {
// bouding box optimization by ge1doot.
if (Math.abs(this.aabb[0] - p.aabb[0]) - (p.aabb[2] + this.aabb[2]) >= 0 &&
Math.abs(this.aabb[1] - p.aabb[1]) - (p.aabb[3] + this.aabb[3]) >= 0) return this.dp.length > 0;
// polygon diff algorithm (narrow phase)
const ndp = [];
for (let i = 0, l = this.dp.length; i < l; i+=2) {
const ls0 = this.dp[i];
const ls1 = this.dp[i + 1];
// find all intersections with clip path
const int = [];
for (let j = 0, cl = p.cp.length; j < cl; j++) {
const pint = this.segment_intersect(ls0, ls1, p.cp[j], p.cp[(j + 1) % cl]);
if (pint !== false) {
int.push(pint);
}
}
if (int.length === 0) {
// 0 intersections, inside or outside?
if (diff === !p.inside(ls0)) {
ndp.push(ls0, ls1);
}
} else {
int.push(ls0, ls1);
// order intersection points on line ls.p1 to ls.p2
const cmpx = ls1[0] - ls0[0];
const cmpy = ls1[1] - ls0[1];
int.sort( (a,b) => (a[0] - ls0[0]) * cmpx + (a[1] - ls0[1]) * cmpy -
(b[0] - ls0[0]) * cmpx - (b[1] - ls0[1]) * cmpy);
for (let j = 0; j < int.length - 1; j++) {
if ((int[j][0] - int[j+1][0])**2 + (int[j][1] - int[j+1][1])**2 >= 0.001) {
if (diff === !p.inside([(int[j][0]+int[j+1][0])/2,(int[j][1]+int[j+1][1])/2])) {
ndp.push(int[j], int[j+1]);
}
}
}
}
}
return (this.dp = ndp).length > 0;
}
//port of http://paulbourke.net/geometry/pointlineplane/Helpers.cs
segment_intersect(l1p1, l1p2, l2p1, l2p2) {
const d = (l2p2[1] - l2p1[1]) * (l1p2[0] - l1p1[0]) - (l2p2[0] - l2p1[0]) * (l1p2[1] - l1p1[1]);
if (d === 0) return false;
const n_a = (l2p2[0] - l2p1[0]) * (l1p1[1] - l2p1[1]) - (l2p2[1] - l2p1[1]) * (l1p1[0] - l2p1[0]);
const n_b = (l1p2[0] - l1p1[0]) * (l1p1[1] - l2p1[1]) - (l1p2[1] - l1p1[1]) * (l1p1[0] - l2p1[0]);
const ua = n_a / d;
const ub = n_b / d;
if (ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1) {
return [l1p1[0] + ua * (l1p2[0] - l1p1[0]), l1p1[1] + ua * (l1p2[1] - l1p1[1])];
}
return false;
}
};
return {
list: () => polygonList,
create: () => new Polygon(),
draw: (turtle, p, addToVisList=true) => {
for (let j = 0; j < polygonList.length && p.boolean(polygonList[j]); j++);
p.draw(turtle);
if (addToVisList) polygonList.push(p);
}
};
}