Commit fbdbbe56 authored by Nicaudie Charlotte's avatar Nicaudie Charlotte

Added init function and injectable test code for iter + or

parent c4c88591
// =========== CANVAS SETUP =========== //
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
......@@ -7,21 +8,11 @@ const ctx = canvas.getContext('2d');
const width = canvas.width = window.innerWidth;
const height = canvas.height = window.innerHeight;
// let STARTING_POSITION_X = random(0, 1 * width); // random(%f,%f) avec les 2 premiers floats du INIT
// let STARTING_POSITION_Y = random(0, 1 * height); // random(%f,%f) avec les 2 derniers floats du INIT
let STARTING_POSITION_X = 0;//width / 2; // random(%f,%f) avec les 2 premiers floats du INIT
let STARTING_POSITION_Y = 0;//height / 2; // random(%f,%f) avec les 2 derniers floats du INIT
let ORIENTATION_X = 1;
let ORIENTATION_Y = 0;
let START_POSITION_X = 0;
let START_POSITION_Y = 0;
// =========== UTILS FUNCTIONS =========== //
function random (minValue, maxValue) {
return Math.floor(Math.random() * (maxValue - minValue)) + minValue;
}
let BASE_ORIENTATION_X = 1;
let BASE_ORIENTATION_Y = 0;
// =========== PEN DEFINITION =========== //
......@@ -33,12 +24,8 @@ function Pen(xPosition, yPosition, xOrientation, yOrientation) {
Pen.prototype.translate = function (distance) {
let newDirection = [this.orientation[0] * distance, this.orientation[1] * distance];
console.log(this.position[0]);
console.log(this.position[1]);
this.position[0] += newDirection[0];
this.position[1] += newDirection[1];
console.log(this.position[0]);
console.log(this.position[1]);
ctx.lineTo(this.position[0], this.position[1]);
}
......@@ -58,43 +45,73 @@ Pen.prototype.rotate = function (angle) {
this.orientation = [+(normalizedOrientationX.toFixed(2)), +(normalizedOrientationY.toFixed(2))];
}
/*
// Iter
function iter(minIter, maxIter) {
let nbIterations = random(minIter, maxIter);
// =========== UTILS FUNCTIONS =========== //
for (let i = 0 ; i < nbIterations ; i++) {
// %a sur la stmt qui est dans Iter
}
/**
* Builds a random value between min and max values
*
* @param {*} minValue minimum bound
* @param {*} maxValue maximum bound
* @returns A float between minValue and maxValue
*/
function random (minValue, maxValue) {
return Math.floor(Math.random() * (maxValue - minValue)) + minValue;
}
// Or
function Or()
{
let orProbability = 0.5; // TODO : mettre % (this is the left-member's random weightening)a
let randomNb = random(0, 1);
if (randomNb < orProbability)
/**
* Initialize the starting position of the Pen.
*
* Float range for XY coordinates
* X between x1 and x2
* Y between y1 and y2
*
* @param {*} x1 X min
* @param {*} x2 X max
* @param {*} y1 Y min
* @param {*} y2 Y max
*/
function init (x1, x2, y1, y2) {
let xMin = Math.min(x1, x2);
let xMax = Math.max(x1, x2);
let yMin = Math.min(y1, y2);
let yMax = Math.max(y1, y2);
// Apply value correction if necessary
if ((xMin > width) || (xMin < 0))
{
console.warn("Choosen x1 is out of the screen !");
xMin = random(0, width / 2);
}
else if ((xMax > width) || (xMax < 0))
{
// Apply stmt1
}
else
console.warn("Choosen x2 is out of the screen !");
xMax = random(width / 2, width);
}
else if ((yMin > height) || (yMin < 0))
{
// Apply stmt2
console.warn("Choosen y1 is out of the screen !");
yMin = random(0, height / 2);
}
else if ((yMax > height) || (yMax < 0))
{
console.warn("Choosen y2 is out of the screen !");
yMax = random(height / 2, height);
}
// Apply starting position
START_POSITION_X = random(xMin, xMax);
START_POSITION_Y = random(yMin, yMax);
}
*/
// =========== MAIN DRAWING =========== //
// Init start positions
init(0.0, 300.0, 0.0, 300.0); // TODO : replace with %f
// Create a pen
let pen = new Pen(STARTING_POSITION_X, STARTING_POSITION_Y, ORIENTATION_X, ORIENTATION_Y);
let pen = new Pen(START_POSITION_X, START_POSITION_Y, BASE_ORIENTATION_X, BASE_ORIENTATION_Y);
// BEGIN OF THE DRAWING
console.log("Width = " + width);
console.log("Height = " + height);
ctx.lineWidth = 6;
ctx.beginPath();
......@@ -102,8 +119,22 @@ ctx.moveTo(pen.position[0], pen.position[1]); // Go to init coordinates
// === BEGIN OF CHANGING PART
pen.rotate(45);
pen.translate(484);
for (let i = 0 ; i < random(0, 10); i++) { // TODO ITER : replace 0 and 10 nb of iteration with %f
// Stmt list to inject
pen.rotate(45);
pen.translate(10);
}
if (Math.random() < 0.5) // TODO OR : replace 0.5 proba with %f
{
// Inject Stmt List 1
pen.translate(100);
}
else
{
// Inject Stmt List 2
pen.translate(10);
}
// === END OF CHANGING PART
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment