/*
  Copyright (c) 2021 Corinne Diakhoumpa et Erwan Iev-Le Tac

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
*/

const fs = require("fs");
const utils = require('./utils');

// This program draws the schema associated to "Excentricité".

const a = 229;
const b = 221;
const c = 60;
const font_size = 56;
const scale = 5.1;

const text_around_ellipse =
"\
Une philosophe néoplatonicienne\n\
Sage conseillère pour les antiques maîtres !\n\
Constructrice d’astrolabes et d’hydromètres,\n\
\n\
\n\
\n\
\n\
Elle transmit l’arithmétique diophantienne,\n\
L’œuvre de Ptolémée et les sections coniques.\n\
Elle enseigna littérature, astronomie,\n\
Mathématiques au peuple d’Alexandrie.\n\
Femme forte au sein de la cité hellénique,\n\
\n\
\n\
\n\
\n\
La païenne fut atrocement assassinée\n\
Par le feu d’une meute chrétienne enragée.\n\
".trim().split("\n");

const sun_size = 10;
const sun_ray_count = text_around_ellipse.length;

const filename = "excentricite.svg";
console.log(`Generate ${filename}...`)
let stream = fs.createWriteStream(filename);
stream.write(utils.svgProlog('height="10.756915cm" width="7.950995cm" viewBox="0 0 1590.199 2151.383"'));
stream.write(utils.svgHeaderWithCopyLeft("Excentricité", "Une ellipse de demi-grand axe a = 3;49' et de demi-petit axe b = 3;41' est représentée. Le soleil est placé à un des foyers, à une distance du centre de l'ellipse égale à c = 1. Il est aussi indiqué que l'excentricité e de l'ellipse satisfait 1/e = 229'. Enfin tout autour de l'ellipse 6 alexandrins dessinent des rayons de texte."));
stream.write(`<g style="font-family:'EB Garamond'; font-size: ${font_size}px" transform="translate(-393.1867701474161, -76.25822032211907) translate(${scale*a}, ${scale*b})">`);

// Draw ellipse.
stream.write(`<ellipse rx="${a}" ry="${b}" fill="none" stroke="black"/>`);

// Draw semi-major axis.
stream.write(`<line x1="0" y1="0" x2="${a}" y2="0" stroke="black" stroke-dasharray="5"/>`);

// Draw semi-minor axis.
stream.write(`<line x1="0" y1="0" x2="0" y2="${-b}" stroke="black" stroke-dasharray="5"/>`);

// Draw sun.
stream.write(`<g transform="translate(-${c}, 0)">`);
stream.write(`<circle r="${sun_size}" fill="gray"/>`);
for (let i = 0; i < sun_ray_count; i++) {
    let x = 3*sun_size*Math.cos(2*(.5+i)*Math.PI/sun_ray_count);
    let y = 3*sun_size*Math.sin(2*(.5+i)*Math.PI/sun_ray_count);
    stream.write(`<line x1="0" y1="0" x2="${x}" y2="${y}" stroke="gray"/>`);
}
stream.write(`</g>`);

// Draw distance from center to sun.
stream.write(`<line x2="${-c}" y2="0" stroke="black" stroke-dasharray="5"/>`);

// Draw the text around the ellipse.
stream.write(`<g style="font-size: ${font_size}px">`)
for (let i = 0; i < text_around_ellipse.length; i++) {
    if (!text_around_ellipse[i].length)
        continue;
    let theta = -Math.PI/2 + 2 * Math.PI * (i+.1) / text_around_ellipse.length;
    let A = Math.cos(theta);
    let B = b * Math.sin(theta) / a;
    let C = - a * Math.sin(theta) / b;
    let D = Math.cos(theta);
    stream.write(`<g transform="matrix(${A} ${B} ${C} ${D} 0 0)">`);
    stream.write(`<text x="${a}" y="${font_size/2}px">${text_around_ellipse[i]}</text>`);
    stream.write("</g>");
}
stream.write("</g>");

stream.write("</g>");
stream.write("</svg>")
stream.end();
console.log("done")