#include "library.h" const double pi = acos(-1.0); double distance(double ax, double ay, double bx, double by) { return sqrt((ax-bx)*(ax-bx) + (ay-by)*(ay-by)); } double random_in_range(double a, double b) { return random_in_range((int)(a * 10000), (int)(b * 10000)) / 10000.0; } double random_angle(double mindeg, double maxdeg) { return random_in_range(mindeg, maxdeg) * pi / 180; } void trunk(double p1x, double p1y, double p2x, double p2y, double beta, double len, double taper) { double base = distance(p1x, p1y, p2x, p2y); double alpha = direction_from_to_in_radians(p1x, p1y, p2x, p2y) - pi/2; if (sin(beta) > 2 * len / base) { print("Bad sin(beta)\n"); return; } double p3x = (p1x + p2x) / 2, p3y = (p1y + p2y) / 2; double p4x = p3x + len * sin(alpha + beta), p4y = p3y - len * cos(alpha + beta); double end = base * taper; double g = pi/2 - alpha - beta; double p5x = p4x - 0.5 * end * sin(g), p5y = p4y - 0.5 * end * cos(g); double p6x = p4x + 0.5 * end * sin(g), p6y = p4y + 0.5 * end * cos(g); move_to(p1x, p1y); draw_to(p5x, p5y); draw_to(p6x, p6y); draw_to(p2x, p2y); draw_to(p1x, p1y); } void main() { make_window(800, 800); trunk( 50, 780, 150, 780, 0, 700, 0.5); trunk(250, 780, 350, 780, 15*pi/180, 700, 0.5); trunk(550, 780, 650, 720, 20*pi/180, 300, 0.3); }