2020-12-22 00:01:36 +00:00
|
|
|
|
/**
|
|
|
|
|
* Logarithm Graph Creator - Create graphs with logarithm scales.
|
|
|
|
|
* Copyright (C) 2020 Ad5001
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
.pragma library
|
|
|
|
|
|
|
|
|
|
.import "utils.js" as Utils
|
|
|
|
|
.import "mathlib.js" as MathLib
|
2020-12-26 13:49:48 +00:00
|
|
|
|
.import "parameters.js" as P
|
2020-12-22 00:01:36 +00:00
|
|
|
|
|
|
|
|
|
|
2020-12-23 23:28:06 +00:00
|
|
|
|
function getNewName(allowedLetters) {
|
|
|
|
|
var newid = 0
|
|
|
|
|
var ret
|
|
|
|
|
do {
|
|
|
|
|
var letter = allowedLetters[newid % allowedLetters.length]
|
|
|
|
|
var num = Math.floor((newid - (newid % allowedLetters.length)) / allowedLetters.length)
|
2020-12-24 00:31:57 +00:00
|
|
|
|
ret = letter + (num > 0 ? Utils.textsub(num-1) : '')
|
2020-12-23 23:28:06 +00:00
|
|
|
|
newid += 1
|
|
|
|
|
} while(getObjectByName(ret) != null)
|
|
|
|
|
return ret
|
2020-12-22 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class DrawableObject {
|
2020-12-22 23:23:38 +00:00
|
|
|
|
// Class to extend for every type of object that
|
|
|
|
|
// can be drawn on the canvas.
|
2020-12-22 00:01:36 +00:00
|
|
|
|
static type(){return 'Unknown'}
|
2020-12-22 23:23:38 +00:00
|
|
|
|
// Label used for the list on the ObjectsList sidebar.
|
2020-12-22 20:08:45 +00:00
|
|
|
|
static typeMultiple(){return 'Unknown'}
|
2020-12-22 23:23:38 +00:00
|
|
|
|
// Whether this object can be created by the user
|
|
|
|
|
// or are instanciated by other objects.
|
2020-12-22 20:08:45 +00:00
|
|
|
|
static createable() {return true}
|
2020-12-22 23:23:38 +00:00
|
|
|
|
// Properties are set with key as property name and
|
2020-12-26 18:16:42 +00:00
|
|
|
|
// value as it's type name (e.g 'Expression', 'string'...),
|
|
|
|
|
// an Enum for enumerations, an ObjectType for DrawableObjects
|
|
|
|
|
// with a specific type, a List instance for lists, a
|
|
|
|
|
// Dictionary instance for dictionaries...
|
2020-12-22 23:23:38 +00:00
|
|
|
|
// Used for property modifier in the sidebar.
|
2020-12-22 00:01:36 +00:00
|
|
|
|
static properties() {return {}}
|
|
|
|
|
|
|
|
|
|
constructor(name, visible = true, color = null, labelContent = 'name + value') {
|
2020-12-22 23:23:38 +00:00
|
|
|
|
if(color == null) color = Utils.getRandomColor()
|
2020-12-22 00:01:36 +00:00
|
|
|
|
this.type = 'Unknown'
|
|
|
|
|
this.name = name
|
|
|
|
|
this.visible = visible
|
|
|
|
|
this.color = color
|
|
|
|
|
this.labelContent = labelContent // "null", "name", "name + value"
|
|
|
|
|
this.requiredBy = []
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 15:07:27 +00:00
|
|
|
|
export() {
|
|
|
|
|
// Should return what will be input as arguments when a file is loaded (serializable form)
|
|
|
|
|
return [this.name, this.visible, this.color.toString(), this.labelContent]
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 00:01:36 +00:00
|
|
|
|
getReadableString() {
|
|
|
|
|
return `${this.name} = Unknown`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getLabel() {
|
|
|
|
|
switch(this.labelContent) {
|
|
|
|
|
case 'name':
|
|
|
|
|
return this.name
|
|
|
|
|
case 'name + value':
|
|
|
|
|
return this.getReadableString()
|
|
|
|
|
case 'null':
|
|
|
|
|
return ''
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 15:07:27 +00:00
|
|
|
|
update() {
|
2020-12-25 22:42:31 +00:00
|
|
|
|
for(var req of this.requiredBy) {
|
|
|
|
|
req.update()
|
2020-12-24 15:07:27 +00:00
|
|
|
|
}
|
2020-12-22 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 15:07:27 +00:00
|
|
|
|
delete() {
|
2020-12-25 22:42:31 +00:00
|
|
|
|
for(var toRemove of this.requiredBy) {
|
2020-12-24 15:07:27 +00:00
|
|
|
|
toRemove.delete()
|
|
|
|
|
currentObjects[toRemove.type] = currentObjects[toRemove.type].filter(obj => obj.name != toRemove.name)
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-22 20:08:45 +00:00
|
|
|
|
|
2020-12-22 00:01:36 +00:00
|
|
|
|
draw(canvas, ctx) {}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 18:14:37 +00:00
|
|
|
|
|
2020-12-22 23:23:38 +00:00
|
|
|
|
class ExecutableObject extends DrawableObject {
|
|
|
|
|
// Class to be extended for every class upon which we
|
|
|
|
|
// calculate an y for a x with the execute function.
|
|
|
|
|
// If a value cannot be found during execute, it should
|
|
|
|
|
// return null. However, theses values should
|
|
|
|
|
// return false when passed to canExecute.
|
|
|
|
|
execute(x = 1) {return 0}
|
|
|
|
|
canExecute(x = 1) {return true}
|
|
|
|
|
// Simplify returns the simplified string of the expression.
|
|
|
|
|
simplify(x = 1) {return '0'}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 18:14:37 +00:00
|
|
|
|
|
2020-12-22 00:01:36 +00:00
|
|
|
|
class Point extends DrawableObject {
|
|
|
|
|
static type(){return 'Point'}
|
2020-12-22 20:08:45 +00:00
|
|
|
|
static typeMultiple(){return 'Points'}
|
2020-12-22 00:01:36 +00:00
|
|
|
|
static properties() {return {
|
|
|
|
|
'x': 'Expression',
|
|
|
|
|
'y': 'Expression',
|
2020-12-26 13:49:48 +00:00
|
|
|
|
'labelPosition': new P.Enum('top', 'bottom', 'left', 'right', 'top-left', 'top-right', 'bottom-left', 'bottom-right'),
|
|
|
|
|
'pointStyle': new P.Enum('●', '✕', '+'),
|
2020-12-22 00:01:36 +00:00
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
constructor(name = null, visible = true, color = null, labelContent = 'name + value',
|
2020-12-24 15:07:27 +00:00
|
|
|
|
x = 1, y = 0, labelPosition = 'top', pointStyle = '●') {
|
2020-12-23 23:28:06 +00:00
|
|
|
|
if(name == null) name = getNewName('ABCDEFJKLMNOPQRSTUVW')
|
2020-12-22 00:01:36 +00:00
|
|
|
|
super(name, visible, color, labelContent)
|
|
|
|
|
this.type = 'Point'
|
|
|
|
|
if(typeof x == 'number' || typeof x == 'string') x = new MathLib.Expression(x.toString())
|
|
|
|
|
this.x = x
|
|
|
|
|
if(typeof y == 'number' || typeof y == 'string') y = new MathLib.Expression(y.toString())
|
|
|
|
|
this.y = y
|
2020-12-24 15:07:27 +00:00
|
|
|
|
this.labelPosition = labelPosition
|
2020-12-22 00:01:36 +00:00
|
|
|
|
this.pointStyle = pointStyle
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getReadableString() {
|
|
|
|
|
return `${this.name} = (${this.x}, ${this.y})`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export() {
|
2020-12-24 15:07:27 +00:00
|
|
|
|
return [this.name, this.visible, this.color.toString(), this.labelContent, this.x.toEditableString(), this.y.toEditableString(), this.labelPosition, this.pointStyle]
|
2020-12-22 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
draw(canvas, ctx) {
|
2020-12-22 23:23:38 +00:00
|
|
|
|
var [canvasX, canvasY] = [canvas.x2px(this.x.execute()), canvas.y2px(this.y.execute())]
|
2020-12-22 00:01:36 +00:00
|
|
|
|
var pointSize = 8
|
|
|
|
|
switch(this.pointStyle) {
|
2020-12-24 00:31:57 +00:00
|
|
|
|
case '●':
|
2020-12-22 00:01:36 +00:00
|
|
|
|
ctx.beginPath();
|
|
|
|
|
ctx.ellipse(canvasX-pointSize/2, canvasY-pointSize/2, pointSize, pointSize)
|
|
|
|
|
ctx.fill();
|
|
|
|
|
break;
|
2020-12-24 00:31:57 +00:00
|
|
|
|
case '✕':
|
2020-12-22 00:01:36 +00:00
|
|
|
|
canvas.drawLine(ctx, canvasX-pointSize/2, canvasY-pointSize/2, canvasX+pointSize/2, canvasY+pointSize/2)
|
2020-12-24 00:31:57 +00:00
|
|
|
|
canvas.drawLine(ctx, canvasX-pointSize/2, canvasY+pointSize/2, canvasX+pointSize/2, canvasY-pointSize/2)
|
2020-12-22 00:01:36 +00:00
|
|
|
|
break;
|
2020-12-24 00:31:57 +00:00
|
|
|
|
case '+':
|
2020-12-25 18:30:19 +00:00
|
|
|
|
ctx.fillRect(canvasX-pointSize/2, canvasY-1, pointSize, 2)
|
|
|
|
|
ctx.fillRect(canvasX-1, canvasY-pointSize/2, 2, pointSize)
|
2020-12-22 00:01:36 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
var text = this.getLabel()
|
|
|
|
|
ctx.font = "14px sans-serif"
|
|
|
|
|
var textSize = ctx.measureText(text).width
|
2020-12-24 15:07:27 +00:00
|
|
|
|
switch(this.labelPosition) {
|
2020-12-22 00:01:36 +00:00
|
|
|
|
case 'top':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, canvasX-textSize/2, canvasY-16)
|
|
|
|
|
break;
|
|
|
|
|
case 'bottom':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, canvasX-textSize/2, canvasY+16)
|
|
|
|
|
break;
|
|
|
|
|
case 'left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, canvasX-textSize-10, canvasY+4)
|
|
|
|
|
break;
|
|
|
|
|
case 'right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, canvasX+10, canvasY+4)
|
|
|
|
|
break;
|
2020-12-24 18:14:37 +00:00
|
|
|
|
case 'top-left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, canvasX-textSize-10, canvasY-16)
|
|
|
|
|
break;
|
|
|
|
|
case 'top-right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, canvasX+10, canvasY-16)
|
|
|
|
|
break;
|
|
|
|
|
case 'bottom-left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, canvasX-textSize-10, canvasY+16)
|
|
|
|
|
break;
|
|
|
|
|
case 'bottom-right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, canvasX+10, canvasY+16)
|
|
|
|
|
break;
|
2020-12-22 00:01:36 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 18:14:37 +00:00
|
|
|
|
|
2020-12-22 23:23:38 +00:00
|
|
|
|
class Function extends ExecutableObject {
|
2020-12-22 00:01:36 +00:00
|
|
|
|
static type(){return 'Function'}
|
2020-12-22 20:08:45 +00:00
|
|
|
|
static typeMultiple(){return 'Functions'}
|
2020-12-22 00:01:36 +00:00
|
|
|
|
static properties() {return {
|
|
|
|
|
'expression': 'Expression',
|
2021-01-05 09:54:59 +00:00
|
|
|
|
'definitionDomain': 'Domain',
|
|
|
|
|
'destinationDomain': 'Domain',
|
|
|
|
|
'comment1': 'Ex: R+* (ℝ⁺*), N (ℕ), Z-* (ℤ⁻*), ]0;1[, {3;4;5}',
|
|
|
|
|
'labelPosition': new P.Enum('above', 'below', 'left', 'right', 'above-left', 'above-right', 'below-left', 'below-right'),
|
2020-12-26 13:49:48 +00:00
|
|
|
|
'displayMode': new P.Enum('application', 'function'),
|
2020-12-26 18:16:42 +00:00
|
|
|
|
'labelX': 'number',
|
2021-01-05 09:54:59 +00:00
|
|
|
|
'comment2': 'The following parameters are used when the definition domain is a non-continuous set. (Ex: ℕ, ℤ, sets like {0;3}...)',
|
2020-12-26 18:16:42 +00:00
|
|
|
|
'drawPoints': 'Boolean',
|
|
|
|
|
'drawDashedLines': 'Boolean'
|
2020-12-22 00:01:36 +00:00
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
constructor(name = null, visible = true, color = null, labelContent = 'name + value',
|
2021-01-05 09:54:59 +00:00
|
|
|
|
expression = 'x', definitionDomain = 'RPE', destinationDomain = 'R',
|
2020-12-26 18:16:42 +00:00
|
|
|
|
displayMode = 'application', labelPosition = 'above', labelX = 1,
|
|
|
|
|
drawPoints = true, drawDashedLines = true) {
|
2020-12-23 23:28:06 +00:00
|
|
|
|
if(name == null) name = getNewName('fghjqlmnopqrstuvwabcde')
|
2020-12-22 00:01:36 +00:00
|
|
|
|
super(name, visible, color, labelContent)
|
2020-12-24 15:07:27 +00:00
|
|
|
|
this.type = 'Function'
|
2020-12-22 00:01:36 +00:00
|
|
|
|
if(typeof expression == 'number' || typeof expression == 'string') expression = new MathLib.Expression(expression.toString())
|
|
|
|
|
this.expression = expression
|
2021-01-05 09:54:59 +00:00
|
|
|
|
if(typeof definitionDomain == 'string') definitionDomain = MathLib.parseDomain(definitionDomain)
|
|
|
|
|
this.definitionDomain = definitionDomain
|
|
|
|
|
if(typeof destinationDomain == 'string') destinationDomain = MathLib.parseDomain(destinationDomain)
|
|
|
|
|
this.destinationDomain = destinationDomain
|
2020-12-22 00:01:36 +00:00
|
|
|
|
this.displayMode = displayMode
|
2020-12-24 15:07:27 +00:00
|
|
|
|
this.labelPosition = labelPosition
|
2020-12-22 00:01:36 +00:00
|
|
|
|
this.labelX = labelX
|
2020-12-26 18:16:42 +00:00
|
|
|
|
this.drawPoints = drawPoints
|
|
|
|
|
this.drawDashedLines = drawDashedLines
|
2020-12-22 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getReadableString() {
|
|
|
|
|
if(this.displayMode == 'application') {
|
2021-03-08 22:12:10 +00:00
|
|
|
|
return `${this.name}: ${this.definitionDomain} ⟼ ${this.destinationDomain}\n ${' '.repeat(this.name.length)}x ⟼ ${this.expression.toString()}`
|
2020-12-22 00:01:36 +00:00
|
|
|
|
} else {
|
2021-01-10 19:48:11 +00:00
|
|
|
|
return `${this.name}(x) = ${this.expression.toString()}\nD${Utils.textsub(this.name)} = ${this.definitionDomain}`
|
2020-12-22 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export() {
|
|
|
|
|
return [this.name, this.visible, this.color.toString(), this.labelContent,
|
2021-01-05 09:54:59 +00:00
|
|
|
|
this.expression.toEditableString(), this.definitionDomain.toString(), this.destinationDomain.toString(),
|
2020-12-26 18:16:42 +00:00
|
|
|
|
this.displayMode, this.labelPosition, this.labelX, this.drawPoints, this.drawDashedLines]
|
2020-12-22 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 23:23:38 +00:00
|
|
|
|
execute(x = 1) {
|
2021-01-05 09:54:59 +00:00
|
|
|
|
if(this.definitionDomain.includes(x))
|
2020-12-24 15:07:27 +00:00
|
|
|
|
return this.expression.execute(x)
|
2020-12-22 23:23:38 +00:00
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
canExecute(x = 1) {
|
2021-01-05 09:54:59 +00:00
|
|
|
|
return this.definitionDomain.includes(x)
|
2020-12-22 23:23:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
simplify(x = 1) {
|
2021-01-05 09:54:59 +00:00
|
|
|
|
if(this.definitionDomain.includes(x))
|
2020-12-24 15:07:27 +00:00
|
|
|
|
return this.expression.simplify(x)
|
2020-12-22 23:23:38 +00:00
|
|
|
|
return ''
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 00:01:36 +00:00
|
|
|
|
draw(canvas, ctx) {
|
2021-01-05 09:54:59 +00:00
|
|
|
|
Function.drawFunction(canvas, ctx, this.expression, this.definitionDomain, this.destinationDomain, this.drawPoints, this.drawDashedLines)
|
2020-12-22 00:01:36 +00:00
|
|
|
|
// Label
|
|
|
|
|
var text = this.getLabel()
|
|
|
|
|
ctx.font = "14px sans-serif"
|
2020-12-24 18:14:37 +00:00
|
|
|
|
var textSize = canvas.measureText(ctx, text, 7)
|
2020-12-22 00:01:36 +00:00
|
|
|
|
var posX = canvas.x2px(this.labelX)
|
2020-12-26 18:16:42 +00:00
|
|
|
|
var posY = canvas.y2px(this.execute(this.labelX))
|
2020-12-24 15:07:27 +00:00
|
|
|
|
switch(this.labelPosition) {
|
2020-12-22 00:01:36 +00:00
|
|
|
|
case 'above':
|
2021-01-05 09:54:59 +00:00
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width/2, posY-textSize.height)
|
2020-12-22 00:01:36 +00:00
|
|
|
|
break;
|
|
|
|
|
case 'below':
|
2021-01-05 09:54:59 +00:00
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width/2, posY+textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY-textSize.height/2)
|
|
|
|
|
break;
|
|
|
|
|
case 'right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX, posY-textSize.height/2)
|
|
|
|
|
break;
|
|
|
|
|
case 'above-left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY-textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'above-right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX, posY-textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'below-left':
|
2020-12-25 18:30:19 +00:00
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY+textSize.height)
|
2020-12-22 00:01:36 +00:00
|
|
|
|
break;
|
2021-01-05 09:54:59 +00:00
|
|
|
|
case 'below-right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX, posY+textSize.height)
|
|
|
|
|
break;
|
2020-12-22 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-22 15:47:48 +00:00
|
|
|
|
|
2021-01-05 09:54:59 +00:00
|
|
|
|
static drawFunction(canvas, ctx, expr, definitionDomain, destinationDomain, drawPoints = true, drawDash = true) {
|
2020-12-22 15:47:48 +00:00
|
|
|
|
// Reusable in other objects.
|
|
|
|
|
// Drawing small traits every 2px
|
|
|
|
|
var pxprecision = 2
|
|
|
|
|
var previousX = canvas.px2x(0)
|
2020-12-25 18:30:19 +00:00
|
|
|
|
var previousY;
|
2021-01-05 09:54:59 +00:00
|
|
|
|
if(definitionDomain instanceof MathLib.SpecialDomain && definitionDomain.moveSupported) {
|
2020-12-26 18:16:42 +00:00
|
|
|
|
// Point based functions.
|
2021-01-05 09:54:59 +00:00
|
|
|
|
previousX = definitionDomain.previous(previousX)
|
|
|
|
|
if(previousX === null) previousX = definitionDomain.next(canvas.px2x(0))
|
2020-12-25 18:30:19 +00:00
|
|
|
|
previousY = expr.execute(previousX)
|
2020-12-26 18:16:42 +00:00
|
|
|
|
if(!drawPoints && !drawDash) return
|
2020-12-25 18:30:19 +00:00
|
|
|
|
while(previousX !== null && canvas.x2px(previousX) < canvas.canvasSize.width) {
|
2021-01-05 09:54:59 +00:00
|
|
|
|
var currentX = definitionDomain.next(previousX)
|
2020-12-25 18:30:19 +00:00
|
|
|
|
var currentY = expr.execute(currentX)
|
|
|
|
|
if(currentX === null) break;
|
2021-01-05 09:54:59 +00:00
|
|
|
|
if((definitionDomain.includes(currentX) || definitionDomain.includes(previousX)) &&
|
|
|
|
|
(destinationDomain.includes(currentY) || destinationDomain.includes(previousY))) {
|
2020-12-26 18:16:42 +00:00
|
|
|
|
if(drawDash)
|
|
|
|
|
canvas.drawDashedLine(ctx, canvas.x2px(previousX), canvas.y2px(previousY), canvas.x2px(currentX), canvas.y2px(currentY))
|
|
|
|
|
if(drawPoints) {
|
|
|
|
|
ctx.fillRect(canvas.x2px(previousX)-5, canvas.y2px(previousY)-1, 10, 2)
|
|
|
|
|
ctx.fillRect(canvas.x2px(previousX)-1, canvas.y2px(previousY)-5, 2, 10)
|
|
|
|
|
}
|
2020-12-22 15:47:48 +00:00
|
|
|
|
}
|
2020-12-25 18:30:19 +00:00
|
|
|
|
previousX = currentX
|
|
|
|
|
previousY = currentY
|
|
|
|
|
}
|
2020-12-26 18:16:42 +00:00
|
|
|
|
if(drawPoints) {
|
|
|
|
|
// Drawing the last cross
|
|
|
|
|
ctx.fillRect(canvas.x2px(previousX)-5, canvas.y2px(previousY)-1, 10, 2)
|
|
|
|
|
ctx.fillRect(canvas.x2px(previousX)-1, canvas.y2px(previousY)-5, 2, 10)
|
|
|
|
|
}
|
2020-12-25 18:30:19 +00:00
|
|
|
|
} else {
|
|
|
|
|
previousY = expr.execute(previousX)
|
|
|
|
|
for(var px = pxprecision; px < canvas.canvasSize.width; px += pxprecision) {
|
|
|
|
|
var currentX = canvas.px2x(px)
|
|
|
|
|
var currentY = expr.execute(currentX)
|
2021-01-05 09:54:59 +00:00
|
|
|
|
if((definitionDomain.includes(currentX) || definitionDomain.includes(previousX)) &&
|
|
|
|
|
(destinationDomain.includes(currentY) || destinationDomain.includes(previousY)) &&
|
2020-12-25 18:30:19 +00:00
|
|
|
|
Math.abs(previousY-currentY)<100) {
|
|
|
|
|
canvas.drawLine(ctx, canvas.x2px(previousX), canvas.y2px(previousY), canvas.x2px(currentX), canvas.y2px(currentY))
|
|
|
|
|
}
|
|
|
|
|
previousX = currentX
|
|
|
|
|
previousY = currentY
|
|
|
|
|
}
|
2020-12-22 15:47:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 18:14:37 +00:00
|
|
|
|
|
2020-12-22 23:23:38 +00:00
|
|
|
|
class GainBode extends ExecutableObject {
|
2020-12-22 15:47:48 +00:00
|
|
|
|
static type(){return 'Gain Bode'}
|
2020-12-22 20:08:45 +00:00
|
|
|
|
static typeMultiple(){return 'Gains Bode'}
|
2020-12-22 15:47:48 +00:00
|
|
|
|
static properties() {return {
|
2020-12-26 13:49:48 +00:00
|
|
|
|
'om_0': new P.ObjectType('Point'),
|
|
|
|
|
'pass': new P.Enum('high', 'low'),
|
2020-12-22 15:47:48 +00:00
|
|
|
|
'gain': 'Expression',
|
2021-01-05 09:54:59 +00:00
|
|
|
|
'labelPosition': new P.Enum('above', 'below', 'left', 'right', 'above-left', 'above-right', 'below-left', 'below-right'),
|
2020-12-22 15:47:48 +00:00
|
|
|
|
'labelX': 'number'
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
constructor(name = null, visible = true, color = null, labelContent = 'name + value',
|
2020-12-24 16:04:22 +00:00
|
|
|
|
om_0 = '', pass = 'high', gain = '20', labelPosition = 'above', labelX = 1) {
|
2020-12-23 23:28:06 +00:00
|
|
|
|
if(name == null) name = getNewName('G')
|
2020-12-24 15:07:27 +00:00
|
|
|
|
if(name == 'G') name = 'G₀' // G is reserved for sum of BODE magnitudes (Somme gains Bode).
|
2020-12-22 15:47:48 +00:00
|
|
|
|
super(name, visible, color, labelContent)
|
2020-12-24 15:07:27 +00:00
|
|
|
|
this.type = 'Gain Bode'
|
2020-12-24 16:04:22 +00:00
|
|
|
|
if(typeof om_0 == "string") {
|
2020-12-22 15:47:48 +00:00
|
|
|
|
// Point name or create one
|
2020-12-24 16:04:22 +00:00
|
|
|
|
om_0 = getObjectByName(om_0, 'Point')
|
|
|
|
|
if(om_0 == null) {
|
2020-12-22 15:47:48 +00:00
|
|
|
|
// Create new point
|
2020-12-24 16:04:22 +00:00
|
|
|
|
om_0 = createNewRegisteredObject('Point')
|
|
|
|
|
om_0.name = getNewName('ω')
|
2020-12-24 18:14:37 +00:00
|
|
|
|
om_0.labelContent = 'name'
|
2020-12-24 16:04:22 +00:00
|
|
|
|
om_0.color = this.color
|
2020-12-24 15:07:27 +00:00
|
|
|
|
labelPosition = 'below'
|
2020-12-22 15:47:48 +00:00
|
|
|
|
}
|
2020-12-24 16:04:22 +00:00
|
|
|
|
om_0.requiredBy.push(this)
|
2020-12-22 15:47:48 +00:00
|
|
|
|
}
|
2020-12-24 16:04:22 +00:00
|
|
|
|
this.om_0 = om_0
|
2020-12-22 15:47:48 +00:00
|
|
|
|
this.pass = pass
|
|
|
|
|
if(typeof gain == 'number' || typeof gain == 'string') gain = new MathLib.Expression(gain.toString())
|
|
|
|
|
this.gain = gain
|
2020-12-24 15:07:27 +00:00
|
|
|
|
this.labelPosition = labelPosition
|
2020-12-22 15:47:48 +00:00
|
|
|
|
this.labelX = labelX
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getReadableString() {
|
2020-12-24 18:14:37 +00:00
|
|
|
|
return `${this.name}: ${this.pass}-pass; ${this.om_0.name} = ${this.om_0.x}\n ${' '.repeat(this.name.length)}${this.gain.toString(true)} dB/dec`
|
2020-12-22 15:47:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export() {
|
|
|
|
|
return [this.name, this.visible, this.color.toString(), this.labelContent,
|
2020-12-24 16:04:22 +00:00
|
|
|
|
this.om_0.name, this.pass.toString(), this.gain.toEditableString(), this.labelPosition, this.labelX]
|
2020-12-22 15:47:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 23:23:38 +00:00
|
|
|
|
execute(x=1) {
|
2020-12-24 16:04:22 +00:00
|
|
|
|
if(typeof x == 'string') x = MathLib.executeExpression(x)
|
|
|
|
|
if((this.pass == 'high' && x < this.om_0.x) || (this.pass == 'low' && x > this.om_0.x)) {
|
|
|
|
|
var dbfn = new MathLib.Expression(`${this.gain.execute()}*(ln(x)-ln(${this.om_0.x}))/ln(10)+${this.om_0.y}`)
|
2020-12-22 23:23:38 +00:00
|
|
|
|
return dbfn.execute(x)
|
2020-12-22 20:08:45 +00:00
|
|
|
|
} else {
|
2020-12-24 16:04:22 +00:00
|
|
|
|
return this.om_0.y.execute()
|
2020-12-22 20:08:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 23:23:38 +00:00
|
|
|
|
simplify(x = 1) {
|
2020-12-24 16:04:22 +00:00
|
|
|
|
var xval = x
|
|
|
|
|
if(typeof x == 'string') xval = MathLib.executeExpression(x)
|
|
|
|
|
if((this.pass == 'high' && xval < this.om_0.x) || (this.pass == 'low' && xval > this.om_0.x)) {
|
|
|
|
|
var dbfn = new MathLib.Expression(`${this.gain.execute()}*(ln(x)-ln(${this.om_0.x}))/ln(10)+${this.om_0.y}`)
|
2020-12-22 23:23:38 +00:00
|
|
|
|
return dbfn.simplify(x)
|
|
|
|
|
} else {
|
2020-12-24 16:04:22 +00:00
|
|
|
|
return this.om_0.y.toString()
|
2020-12-22 23:23:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
canExecute(x = 1) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 15:47:48 +00:00
|
|
|
|
draw(canvas, ctx) {
|
2020-12-24 16:04:22 +00:00
|
|
|
|
var base = [canvas.x2px(this.om_0.x), canvas.y2px(this.om_0.y)]
|
|
|
|
|
var dbfn = new MathLib.Expression(`${this.gain.execute()}*(ln(x)-ln(${this.om_0.x}))/ln(10)+${this.om_0.y}`)
|
2020-12-22 15:47:48 +00:00
|
|
|
|
var inDrawDom = new MathLib.EmptySet()
|
|
|
|
|
|
|
|
|
|
if(this.pass == 'high') {
|
|
|
|
|
// High pass, linear line from begining, then constant to the end.
|
|
|
|
|
canvas.drawLine(ctx, base[0], base[1], canvas.canvasSize.width, base[1])
|
2020-12-24 16:04:22 +00:00
|
|
|
|
inDrawDom = MathLib.parseDomain(`]-inf;${this.om_0.x}[`)
|
2020-12-22 15:47:48 +00:00
|
|
|
|
} else {
|
|
|
|
|
// Low pass, constant from the beginning, linear line to the end.
|
|
|
|
|
canvas.drawLine(ctx, base[0], base[1], 0, base[1])
|
2020-12-24 16:04:22 +00:00
|
|
|
|
inDrawDom = MathLib.parseDomain(`]${this.om_0.x};+inf[`)
|
2020-12-22 15:47:48 +00:00
|
|
|
|
}
|
|
|
|
|
Function.drawFunction(canvas, ctx, dbfn, inDrawDom, MathLib.Domain.R)
|
2020-12-22 17:22:38 +00:00
|
|
|
|
|
|
|
|
|
// Label
|
|
|
|
|
var text = this.getLabel()
|
|
|
|
|
ctx.font = "14px sans-serif"
|
2020-12-24 18:14:37 +00:00
|
|
|
|
var textSize = canvas.measureText(ctx, text, 7)
|
2020-12-22 17:22:38 +00:00
|
|
|
|
var posX = canvas.x2px(this.labelX)
|
2020-12-22 23:23:38 +00:00
|
|
|
|
var posY = canvas.y2px(this.execute(this.labelX))
|
2020-12-24 15:07:27 +00:00
|
|
|
|
switch(this.labelPosition) {
|
2020-12-22 17:22:38 +00:00
|
|
|
|
case 'above':
|
2021-01-05 09:54:59 +00:00
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width/2, posY-textSize.height)
|
2020-12-22 17:22:38 +00:00
|
|
|
|
break;
|
|
|
|
|
case 'below':
|
2021-01-05 09:54:59 +00:00
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width/2, posY+textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY-textSize.height/2)
|
|
|
|
|
break;
|
|
|
|
|
case 'right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX, posY-textSize.height/2)
|
|
|
|
|
break;
|
|
|
|
|
case 'above-left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY-textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'above-right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX, posY-textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'below-left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY+textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'below-right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX, posY+textSize.height)
|
2020-12-22 17:22:38 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2020-12-22 15:47:48 +00:00
|
|
|
|
}
|
2020-12-22 20:08:45 +00:00
|
|
|
|
|
|
|
|
|
update() {
|
2020-12-22 22:19:00 +00:00
|
|
|
|
if(currentObjects['Somme gains Bode'] != undefined) {
|
|
|
|
|
currentObjects['Somme gains Bode'][0].recalculateCache()
|
|
|
|
|
} else {
|
|
|
|
|
createNewRegisteredObject('Somme gains Bode')
|
2020-12-22 20:08:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 18:14:37 +00:00
|
|
|
|
|
2020-12-22 22:19:00 +00:00
|
|
|
|
class SommeGainsBode extends DrawableObject {
|
|
|
|
|
static type(){return 'Somme gains Bode'}
|
|
|
|
|
static typeMultiple(){return 'Somme gains Bode'}
|
|
|
|
|
static createable() {return false}
|
2020-12-22 20:08:45 +00:00
|
|
|
|
static properties() {return {
|
2021-01-05 09:54:59 +00:00
|
|
|
|
'labelPosition': new P.Enum('above', 'below', 'left', 'right', 'above-left', 'above-right', 'below-left', 'below-right'),
|
2020-12-22 20:08:45 +00:00
|
|
|
|
'labelX': 'number'
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
constructor(name = null, visible = true, color = null, labelContent = 'name + value',
|
2020-12-24 15:07:27 +00:00
|
|
|
|
labelPosition = 'above', labelX = 1) {
|
2020-12-22 20:08:45 +00:00
|
|
|
|
if(name == null) name = 'G'
|
|
|
|
|
super(name, visible, color, labelContent)
|
2020-12-24 15:07:27 +00:00
|
|
|
|
this.labelPosition = labelPosition
|
2020-12-22 20:08:45 +00:00
|
|
|
|
this.labelX = labelX
|
|
|
|
|
this.recalculateCache()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export() {
|
2020-12-24 15:07:27 +00:00
|
|
|
|
return [this.name, this.visible, this.color.toString(), this.labelContent, this.labelPosition, this.labelX]
|
2020-12-22 20:08:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getReadableString() {
|
2020-12-22 22:19:00 +00:00
|
|
|
|
return `${this.name} = ${getObjectsName('Gain Bode').join(' + ')}`
|
2020-12-22 20:08:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 23:23:38 +00:00
|
|
|
|
execute(x = 0) {
|
2020-12-25 22:42:31 +00:00
|
|
|
|
for(var [dbfn, inDrawDom] of this.cachedParts) {
|
2020-12-22 23:23:38 +00:00
|
|
|
|
if(inDrawDom.includes(x)) {
|
|
|
|
|
return dbfn.execute(x)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
canExecute(x = 1) {
|
|
|
|
|
return true // Should always be true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
simplify(x = 1) {
|
2020-12-25 22:42:31 +00:00
|
|
|
|
for(var [dbfn, inDrawDom] of this.cachedParts) {
|
2020-12-22 23:23:38 +00:00
|
|
|
|
if(inDrawDom.includes(x)) {
|
|
|
|
|
return dbfn.simplify(x)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ''
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 20:08:45 +00:00
|
|
|
|
recalculateCache() {
|
|
|
|
|
this.cachedParts = []
|
|
|
|
|
// Calculating this is fairly resource expansive so it's cached.
|
|
|
|
|
if(currentObjects['Gain Bode'] != undefined) {
|
|
|
|
|
console.log('Recalculating cache gain')
|
2020-12-24 18:14:37 +00:00
|
|
|
|
// Minimum to draw (can be expended if needed, just not infinite or it'll cause issues.
|
|
|
|
|
var drawMin = 0.001
|
2020-12-22 20:08:45 +00:00
|
|
|
|
|
|
|
|
|
var baseY = 0
|
2020-12-24 18:14:37 +00:00
|
|
|
|
var om0xGains = {100000: 0} // To draw the last part
|
|
|
|
|
var om0xPass = {100000: 'high'} // To draw the last part
|
2020-12-24 16:04:22 +00:00
|
|
|
|
currentObjects['Gain Bode'].forEach(function(gainObj) { // Sorting by their om_0 position.
|
2020-12-24 18:14:37 +00:00
|
|
|
|
var om0x = gainObj.om_0.x.execute()
|
|
|
|
|
if(om0xGains[om0x] == undefined) {
|
|
|
|
|
om0xGains[om0x] = gainObj.gain.execute()
|
|
|
|
|
om0xPass[om0x] = gainObj.pass == 'high'
|
2020-12-22 20:08:45 +00:00
|
|
|
|
} else {
|
2020-12-24 18:14:37 +00:00
|
|
|
|
om0xGains[om0x+0.001] = gainObj.gain.execute()
|
|
|
|
|
om0xPass[om0x+0.001] = gainObj.pass == 'high'
|
2020-12-22 20:08:45 +00:00
|
|
|
|
}
|
2020-12-22 23:23:38 +00:00
|
|
|
|
baseY += gainObj.execute(drawMin)
|
2020-12-22 20:08:45 +00:00
|
|
|
|
})
|
2020-12-24 16:04:22 +00:00
|
|
|
|
// Sorting the om_0x
|
2020-12-24 18:14:37 +00:00
|
|
|
|
var om0xList = Object.keys(om0xGains).map(x => parseFloat(x)) // THEY WERE CONVERTED TO STRINGS...
|
|
|
|
|
om0xList.sort((a,b) => a - b)
|
2020-12-22 20:08:45 +00:00
|
|
|
|
// Adding the total gains.
|
|
|
|
|
var gainsBeforeP = []
|
|
|
|
|
var gainsAfterP = []
|
|
|
|
|
var gainTotal = 0
|
2020-12-25 22:42:31 +00:00
|
|
|
|
for(var om0x of om0xList){
|
|
|
|
|
if(om0xPass[om0x]) { // High-pass
|
|
|
|
|
gainsBeforeP.push(om0xGains[om0x])
|
2020-12-22 20:08:45 +00:00
|
|
|
|
gainsAfterP.push(0)
|
2020-12-25 22:42:31 +00:00
|
|
|
|
gainTotal += om0xGains[om0x] // Gain at first
|
2020-12-22 20:08:45 +00:00
|
|
|
|
} else {
|
|
|
|
|
gainsBeforeP.push(0)
|
2020-12-25 22:42:31 +00:00
|
|
|
|
gainsAfterP.push(om0xGains[om0x])
|
2020-12-22 20:08:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Calculating parts
|
|
|
|
|
var previousPallier = drawMin
|
2020-12-25 22:42:31 +00:00
|
|
|
|
for(var pallier = 0; pallier <= om0xList.length; pallier++) {
|
2020-12-22 20:08:45 +00:00
|
|
|
|
var dbfn = new MathLib.Expression(`${gainTotal}*(ln(x)-ln(${previousPallier}))/ln(10)+${baseY}`)
|
2020-12-24 18:14:37 +00:00
|
|
|
|
var inDrawDom = MathLib.parseDomain(`]${previousPallier};${om0xList[pallier]}]`)
|
2020-12-22 20:08:45 +00:00
|
|
|
|
this.cachedParts.push([dbfn, inDrawDom])
|
2020-12-24 18:14:37 +00:00
|
|
|
|
previousPallier = om0xList[pallier]
|
|
|
|
|
baseY = dbfn.execute(om0xList[pallier])
|
2020-12-24 00:31:57 +00:00
|
|
|
|
gainTotal += gainsAfterP[pallier] - gainsBeforeP[pallier]
|
2020-12-22 20:08:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
draw(canvas, ctx) {
|
|
|
|
|
if(this.cachedParts.length > 0) {
|
2020-12-25 22:42:31 +00:00
|
|
|
|
for(var [dbfn, inDrawDom] of this.cachedParts) {
|
2020-12-22 20:08:45 +00:00
|
|
|
|
Function.drawFunction(canvas, ctx, dbfn, inDrawDom, MathLib.Domain.R)
|
|
|
|
|
if(inDrawDom.includes(this.labelX)) {
|
|
|
|
|
// Label
|
|
|
|
|
var text = this.getLabel()
|
|
|
|
|
ctx.font = "14px sans-serif"
|
2020-12-24 18:14:37 +00:00
|
|
|
|
var textSize = canvas.measureText(ctx, text, 7)
|
2020-12-22 20:08:45 +00:00
|
|
|
|
var posX = canvas.x2px(this.labelX)
|
2020-12-22 23:23:38 +00:00
|
|
|
|
var posY = canvas.y2px(dbfn.execute(this.labelX))
|
2020-12-24 15:07:27 +00:00
|
|
|
|
switch(this.labelPosition) {
|
2020-12-22 20:08:45 +00:00
|
|
|
|
case 'above':
|
2021-01-05 09:54:59 +00:00
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width/2, posY-textSize.height)
|
2020-12-22 20:08:45 +00:00
|
|
|
|
break;
|
|
|
|
|
case 'below':
|
2021-01-05 09:54:59 +00:00
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width/2, posY+textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY-textSize.height/2)
|
|
|
|
|
break;
|
|
|
|
|
case 'right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX, posY-textSize.height/2)
|
|
|
|
|
break;
|
|
|
|
|
case 'above-left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY-textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'above-right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX, posY-textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'below-left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY+textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'below-right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX, posY+textSize.height)
|
2020-12-22 20:08:45 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-22 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 18:14:37 +00:00
|
|
|
|
|
2020-12-22 23:23:38 +00:00
|
|
|
|
class PhaseBode extends ExecutableObject {
|
2020-12-24 15:07:27 +00:00
|
|
|
|
static type(){return 'Phase Bode'}
|
|
|
|
|
static typeMultiple(){return 'Phases Bode'}
|
|
|
|
|
static properties() {return {
|
2020-12-26 13:49:48 +00:00
|
|
|
|
'om_0': new P.ObjectType('Point'),
|
2020-12-24 15:07:27 +00:00
|
|
|
|
'phase': 'Expression',
|
2020-12-26 13:49:48 +00:00
|
|
|
|
'unit': new P.Enum('°', 'deg', 'rad'),
|
2021-01-05 09:54:59 +00:00
|
|
|
|
'labelPosition': new P.Enum('above', 'below', 'left', 'right', 'above-left', 'above-right', 'below-left', 'below-right'),
|
2020-12-24 15:07:27 +00:00
|
|
|
|
'labelX': 'number'
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
constructor(name = null, visible = true, color = null, labelContent = 'name + value',
|
2020-12-24 16:04:22 +00:00
|
|
|
|
om_0 = '', phase = 90, unit = '°', labelPosition = 'above', labelX = 1) {
|
2020-12-24 15:07:27 +00:00
|
|
|
|
if(name == null) name = getNewName('φ')
|
|
|
|
|
if(name == 'φ') name = 'φ₀' // φ is reserved for sum of BODE phases (Somme phases Bode).
|
|
|
|
|
super(name, visible, color, labelContent)
|
|
|
|
|
this.type = 'Phase Bode'
|
2020-12-24 16:04:22 +00:00
|
|
|
|
if(typeof phase == 'number' || typeof phase == 'string') phase = new MathLib.Expression(phase.toString())
|
|
|
|
|
this.phase = phase
|
|
|
|
|
if(typeof om_0 == "string") {
|
2020-12-24 15:07:27 +00:00
|
|
|
|
// Point name or create one
|
2020-12-24 16:04:22 +00:00
|
|
|
|
om_0 = getObjectByName(om_0, 'Point')
|
|
|
|
|
if(om_0 == null) {
|
2020-12-24 15:07:27 +00:00
|
|
|
|
// Create new point
|
2020-12-24 16:04:22 +00:00
|
|
|
|
om_0 = createNewRegisteredObject('Point')
|
|
|
|
|
om_0.name = getNewName('ω')
|
|
|
|
|
om_0.color = this.color
|
2020-12-24 18:14:37 +00:00
|
|
|
|
om_0.labelContent = 'name'
|
2020-12-24 16:04:22 +00:00
|
|
|
|
om_0.labelPosition = this.phase.execute() >= 0 ? 'bottom' : 'top'
|
2020-12-24 15:07:27 +00:00
|
|
|
|
labelPosition = 'below'
|
|
|
|
|
}
|
2020-12-24 16:04:22 +00:00
|
|
|
|
om_0.requiredBy.push(this)
|
2020-12-24 15:07:27 +00:00
|
|
|
|
}
|
2020-12-24 16:04:22 +00:00
|
|
|
|
this.om_0 = om_0
|
2020-12-24 15:07:27 +00:00
|
|
|
|
this.unit = unit
|
|
|
|
|
this.labelPosition = labelPosition
|
|
|
|
|
this.labelX = labelX
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export() {
|
|
|
|
|
return [this.name, this.visible, this.color.toString(), this.labelContent,
|
2020-12-24 16:04:22 +00:00
|
|
|
|
this.om_0.name, this.phase.toEditableString(), this.unit, this.labelPosition, this.labelX]
|
2020-12-24 15:07:27 +00:00
|
|
|
|
}
|
2020-12-22 23:23:38 +00:00
|
|
|
|
|
2020-12-24 15:07:27 +00:00
|
|
|
|
getReadableString() {
|
2021-01-05 09:54:59 +00:00
|
|
|
|
return `${this.name}: ${this.phase.toString(true)}${this.unit} at ${this.om_0.name} = ${this.om_0.x}`
|
2020-12-24 16:04:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
execute(x=1) {
|
|
|
|
|
if(typeof x == 'string') x = MathLib.executeExpression(x)
|
|
|
|
|
if(x < this.om_0.x) {
|
|
|
|
|
return this.om_0.y.execute()
|
|
|
|
|
} else {
|
|
|
|
|
return this.om_0.y.execute() + this.phase.execute()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
simplify(x = 1) {
|
|
|
|
|
var xval = x
|
|
|
|
|
if(typeof x == 'string') xval = MathLib.executeExpression(x)
|
|
|
|
|
if(xval < this.om_0.x) {
|
|
|
|
|
return this.om_0.y.toString()
|
|
|
|
|
} else {
|
|
|
|
|
var newExp = this.om_0.y.toEditableString() + ' + ' + this.phase.toEditableString()
|
|
|
|
|
return (new MathLib.Expression(newExp)).toString()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
canExecute(x = 1) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
draw(canvas, ctx) {
|
|
|
|
|
var baseX = canvas.x2px(this.om_0.x.execute())
|
|
|
|
|
var omy = this.om_0.y.execute()
|
|
|
|
|
var augmt = this.phase.execute()
|
|
|
|
|
var baseY = canvas.y2px(omy)
|
|
|
|
|
var augmtY = canvas.y2px(omy+augmt)
|
2020-12-24 18:14:37 +00:00
|
|
|
|
// Before change line.
|
|
|
|
|
canvas.drawLine(ctx, 0, baseY, Math.min(baseX, canvas.canvasSize.height), baseY)
|
|
|
|
|
// Transition line.
|
|
|
|
|
canvas.drawLine(ctx, baseX, baseY, baseX, augmtY)
|
|
|
|
|
// After change line
|
|
|
|
|
canvas.drawLine(ctx, Math.max(0, baseX), augmtY, canvas.canvasSize.width, augmtY)
|
|
|
|
|
|
|
|
|
|
// Label
|
|
|
|
|
var text = this.getLabel()
|
|
|
|
|
ctx.font = "14px sans-serif"
|
|
|
|
|
var textSize = canvas.measureText(ctx, text, 7)
|
|
|
|
|
var posX = canvas.x2px(this.labelX)
|
|
|
|
|
var posY = canvas.y2px(this.execute(this.labelX))
|
|
|
|
|
switch(this.labelPosition) {
|
|
|
|
|
case 'above':
|
2021-01-05 09:54:59 +00:00
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width/2, posY-textSize.height)
|
2020-12-24 18:14:37 +00:00
|
|
|
|
break;
|
|
|
|
|
case 'below':
|
2021-01-05 09:54:59 +00:00
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width/2, posY+textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY-textSize.height/2)
|
|
|
|
|
break;
|
|
|
|
|
case 'right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX, posY-textSize.height/2)
|
|
|
|
|
break;
|
|
|
|
|
case 'above-left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY-textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'above-right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX, posY-textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'below-left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY+textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'below-right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX, posY+textSize.height)
|
2020-12-24 18:14:37 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
update() {
|
|
|
|
|
if(currentObjects['Somme phases Bode'] != undefined) {
|
|
|
|
|
currentObjects['Somme phases Bode'][0].recalculateCache()
|
|
|
|
|
} else {
|
|
|
|
|
createNewRegisteredObject('Somme phases Bode')
|
2020-12-24 16:04:22 +00:00
|
|
|
|
}
|
2020-12-24 15:07:27 +00:00
|
|
|
|
}
|
2020-12-22 23:23:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 18:14:37 +00:00
|
|
|
|
|
|
|
|
|
class SommePhasesBode extends ExecutableObject {
|
|
|
|
|
static type(){return 'Somme phases Bode'}
|
|
|
|
|
static typeMultiple(){return 'Somme phases Bode'}
|
|
|
|
|
static createable() {return false}
|
|
|
|
|
static properties() {return {
|
2021-01-05 09:54:59 +00:00
|
|
|
|
'labelPosition': new P.Enum('above', 'below', 'left', 'right', 'above-left', 'above-right', 'below-left', 'below-right'),
|
2020-12-24 18:14:37 +00:00
|
|
|
|
'labelX': 'number'
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
constructor(name = null, visible = true, color = null, labelContent = 'name + value',
|
|
|
|
|
labelPosition = 'above', labelX = 1) {
|
|
|
|
|
if(name == null) name = 'φ'
|
|
|
|
|
super(name, visible, color, labelContent)
|
|
|
|
|
this.labelPosition = labelPosition
|
|
|
|
|
this.labelX = labelX
|
|
|
|
|
this.recalculateCache()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export() {
|
|
|
|
|
return [this.name, this.visible, this.color.toString(), this.labelContent, this.labelPosition, this.labelX]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getReadableString() {
|
|
|
|
|
return `${this.name} = ${getObjectsName('Phase Bode').join(' + ')}`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
execute(x=1) {
|
|
|
|
|
if(typeof x == 'string') x = MathLib.executeExpression(x)
|
|
|
|
|
for(var i = 0; i < this.om0xList.length-1; i++) {
|
|
|
|
|
if(x >= this.om0xList[i] && x < this.om0xList[i+1]) return this.phasesList[i]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
simplify(x = 1) {
|
|
|
|
|
var xval = x
|
|
|
|
|
if(typeof x == 'string') xval = MathLib.executeExpression(x)
|
|
|
|
|
for(var i = 0; i < this.om0xList.length-1; i++) {
|
|
|
|
|
if(xval >= this.om0xList[i] && xval < this.om0xList[i+1]) {
|
|
|
|
|
return (new MathLib.Expression(this.phasesExprList[i])).simplify()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return '0'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
canExecute(x = 1) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
recalculateCache() {
|
|
|
|
|
// Minimum to draw (can be expended if needed, just not infinite or it'll cause issues.
|
|
|
|
|
var drawMin = 0.001
|
|
|
|
|
var drawMax = 100000
|
|
|
|
|
this.om0xList = [drawMin, drawMax]
|
|
|
|
|
this.phasesList = [0]
|
|
|
|
|
this.phasesExprList = ['0']
|
|
|
|
|
var phasesDict = {}
|
|
|
|
|
var phasesExprDict = {}
|
|
|
|
|
phasesDict[drawMax] = 0
|
|
|
|
|
|
|
|
|
|
if(currentObjects['Phase Bode'] != undefined) {
|
|
|
|
|
console.log('Recalculating cache phase')
|
2020-12-25 22:42:31 +00:00
|
|
|
|
for(var obj of currentObjects['Phase Bode']) {
|
2020-12-24 18:14:37 +00:00
|
|
|
|
this.om0xList.push(obj.om_0.x.execute())
|
|
|
|
|
if(phasesDict[obj.om_0.x.execute()] == undefined) {
|
|
|
|
|
phasesDict[obj.om_0.x.execute()] = obj.phase.execute()
|
|
|
|
|
phasesExprDict[obj.om_0.x.execute()] = obj.phase.toEditableString()
|
|
|
|
|
} else {
|
|
|
|
|
phasesDict[obj.om_0.x.execute()] += obj.phase.execute()
|
|
|
|
|
phasesExprDict[obj.om_0.x.execute()] += '+' + obj.phase.toEditableString()
|
|
|
|
|
}
|
|
|
|
|
this.phasesList[0] += obj.om_0.y.execute()
|
|
|
|
|
this.phasesExprList[0] += '+' + obj.om_0.y.toEditableString()
|
|
|
|
|
}
|
|
|
|
|
this.om0xList.sort((a,b) => a - b)
|
|
|
|
|
var totalAdded = this.phasesList[0]
|
|
|
|
|
for(var i = 1; i < this.om0xList.length; i++) {
|
|
|
|
|
this.phasesList[i] = this.phasesList[i-1] + phasesDict[this.om0xList[i]]
|
|
|
|
|
this.phasesExprList[i] = this.phasesExprList[i-1] + '+' + phasesDict[this.om0xList[i]]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
draw(canvas, ctx) {
|
|
|
|
|
for(var i = 0; i < this.om0xList.length-1; i++) {
|
|
|
|
|
var om0xBegin = canvas.x2px(this.om0xList[i])
|
|
|
|
|
var om0xEnd = canvas.x2px(this.om0xList[i+1])
|
|
|
|
|
var baseY = canvas.y2px(this.phasesList[i])
|
|
|
|
|
var nextY = canvas.y2px(this.phasesList[i+1])
|
|
|
|
|
canvas.drawLine(ctx, om0xBegin, baseY, om0xEnd, baseY)
|
|
|
|
|
canvas.drawLine(ctx, om0xEnd, baseY, om0xEnd, nextY)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Label
|
|
|
|
|
var text = this.getLabel()
|
|
|
|
|
ctx.font = "14px sans-serif"
|
|
|
|
|
var textSize = canvas.measureText(ctx, text, 7)
|
|
|
|
|
var posX = canvas.x2px(this.labelX)
|
|
|
|
|
var posY = canvas.y2px(this.execute(this.labelX))
|
|
|
|
|
switch(this.labelPosition) {
|
|
|
|
|
case 'above':
|
2021-01-05 09:54:59 +00:00
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width/2, posY-textSize.height)
|
2020-12-24 18:14:37 +00:00
|
|
|
|
break;
|
|
|
|
|
case 'below':
|
2021-01-05 09:54:59 +00:00
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width/2, posY+textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY-textSize.height/2)
|
|
|
|
|
break;
|
|
|
|
|
case 'right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX, posY-textSize.height/2)
|
|
|
|
|
break;
|
|
|
|
|
case 'above-left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY-textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'above-right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX, posY-textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'below-left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY+textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'below-right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX, posY+textSize.height)
|
2020-12-24 18:14:37 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-12-22 23:23:38 +00:00
|
|
|
|
class CursorX extends DrawableObject {
|
2020-12-24 15:07:27 +00:00
|
|
|
|
static type(){return 'X Cursor'}
|
|
|
|
|
static typeMultiple(){return 'X Cursors'}
|
2020-12-22 23:23:38 +00:00
|
|
|
|
static properties() {
|
2020-12-24 00:31:57 +00:00
|
|
|
|
var elementTypes = Object.keys(currentObjects).filter(objType => types[objType].prototype instanceof ExecutableObject)
|
|
|
|
|
var elementNames = ['']
|
2020-12-22 23:23:38 +00:00
|
|
|
|
elementTypes.forEach(function(elemType){
|
|
|
|
|
elementNames = elementNames.concat(currentObjects[elemType].map(obj => obj.name))
|
|
|
|
|
})
|
|
|
|
|
return {
|
|
|
|
|
'x': 'Expression',
|
2020-12-26 13:49:48 +00:00
|
|
|
|
'targetElement': new P.Enum(...elementNames),
|
|
|
|
|
'labelPosition': new P.Enum('left', 'right'),
|
2020-12-24 15:07:27 +00:00
|
|
|
|
'approximate': 'Boolean',
|
|
|
|
|
'rounding': 'number',
|
2020-12-26 13:49:48 +00:00
|
|
|
|
'displayStyle': new P.Enum(
|
2020-12-24 15:07:27 +00:00
|
|
|
|
'— — — — — — —',
|
|
|
|
|
'⸺⸺⸺⸺⸺⸺',
|
|
|
|
|
'• • • • • • • • • •'
|
2020-12-26 13:49:48 +00:00
|
|
|
|
),
|
|
|
|
|
'targetValuePosition' : new P.Enum('Next to target', 'With label', 'Hidden')
|
2020-12-22 23:23:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constructor(name = null, visible = true, color = null, labelContent = 'name + value',
|
2020-12-24 15:07:27 +00:00
|
|
|
|
x = 1, targetElement = null, labelPosition = 'left', approximate = true,
|
|
|
|
|
rounding = 3, displayStyle = '— — — — — — —', targetValuePosition = 'Next to target') {
|
2020-12-24 00:31:57 +00:00
|
|
|
|
if(name == null) name = getNewName('X')
|
2020-12-22 23:23:38 +00:00
|
|
|
|
super(name, visible, color, labelContent)
|
2020-12-24 15:07:27 +00:00
|
|
|
|
this.type = 'X Cursor'
|
|
|
|
|
this.approximate = approximate
|
|
|
|
|
this.rounding = rounding
|
2020-12-22 23:23:38 +00:00
|
|
|
|
if(typeof x == 'number' || typeof x == 'string') x = new MathLib.Expression(x.toString())
|
2020-12-24 00:31:57 +00:00
|
|
|
|
this.x = x
|
2020-12-24 18:14:37 +00:00
|
|
|
|
this.targetElement = targetElement
|
2020-12-24 15:07:27 +00:00
|
|
|
|
this.labelPosition = labelPosition
|
2020-12-24 00:31:57 +00:00
|
|
|
|
this.displayStyle = displayStyle
|
2020-12-24 15:07:27 +00:00
|
|
|
|
this.targetValuePosition = targetValuePosition
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export() {
|
|
|
|
|
return [this.name, this.visible, this.color.toString(), this.labelContent,
|
2020-12-24 18:14:37 +00:00
|
|
|
|
this.x.toEditableString(), this.targetElement, this.labelPosition,
|
2020-12-24 15:07:27 +00:00
|
|
|
|
this.approximate, this.rounding, this.displayStyle, this.targetValuePosition]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getReadableString() {
|
2020-12-24 18:14:37 +00:00
|
|
|
|
if(this.getTargetElement() == null) return `${this.name} = ${this.x.toString()}`
|
2020-12-24 15:07:27 +00:00
|
|
|
|
return `${this.name} = ${this.x.toString()}\n${this.getTargetValueLabel()}`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getTargetValueLabel() {
|
2020-12-24 18:14:37 +00:00
|
|
|
|
var t = this.getTargetElement()
|
2020-12-24 15:07:27 +00:00
|
|
|
|
var approx = ''
|
|
|
|
|
if(this.approximate) {
|
|
|
|
|
approx = t.execute(this.x.execute())
|
|
|
|
|
approx = approx.toPrecision(this.rounding + Math.round(approx).toString().length)
|
|
|
|
|
}
|
|
|
|
|
return `${t.name}(${this.name}) = ${t.simplify(this.x.toEditableString())}` +
|
|
|
|
|
(this.approximate ? ' ≃ ' + approx : '')
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 18:14:37 +00:00
|
|
|
|
getTargetElement() {
|
2020-12-26 18:16:42 +00:00
|
|
|
|
// TODO: Use the dependency system instead.
|
2020-12-24 18:14:37 +00:00
|
|
|
|
var elementTypes = Object.keys(currentObjects).filter(objType => types[objType].prototype instanceof ExecutableObject)
|
|
|
|
|
return getObjectByName(this.targetElement, elementTypes)
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 15:07:27 +00:00
|
|
|
|
getLabel() {
|
|
|
|
|
switch(this.labelContent) {
|
|
|
|
|
case 'name':
|
|
|
|
|
return this.name
|
|
|
|
|
break;
|
|
|
|
|
case 'name + value':
|
|
|
|
|
switch(this.targetValuePosition) {
|
|
|
|
|
case 'Next to target':
|
|
|
|
|
case 'Hidden':
|
|
|
|
|
return `${this.name} = ${this.x.toString()}`
|
|
|
|
|
break;
|
|
|
|
|
case 'With label':
|
|
|
|
|
return this.getReadableString()
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'null':
|
|
|
|
|
return ''
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
draw(canvas, ctx) {
|
|
|
|
|
var xpos = canvas.x2px(this.x.execute())
|
|
|
|
|
switch(this.displayStyle) {
|
|
|
|
|
case '— — — — — — —':
|
|
|
|
|
var dashPxSize = 10
|
|
|
|
|
for(var i = 0; i < canvas.canvasSize.height; i += dashPxSize*2)
|
|
|
|
|
canvas.drawLine(ctx, xpos, i, xpos, i+dashPxSize)
|
|
|
|
|
break;
|
|
|
|
|
case '⸺⸺⸺⸺⸺⸺':
|
|
|
|
|
canvas.drawXLine(ctx, this.x.execute())
|
|
|
|
|
break;
|
|
|
|
|
case '• • • • • • • • • •':
|
|
|
|
|
var pointDistancePx = 10
|
|
|
|
|
var pointSize = 2
|
|
|
|
|
ctx.beginPath();
|
|
|
|
|
for(var i = 0; i < canvas.canvasSize.height; i += pointDistancePx)
|
|
|
|
|
ctx.ellipse(xpos-pointSize/2, i-pointSize/2, pointSize, pointSize)
|
|
|
|
|
ctx.fill();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Label
|
|
|
|
|
var text = this.getLabel()
|
|
|
|
|
ctx.font = "14px sans-serif"
|
|
|
|
|
var textSize = canvas.measureText(ctx, text, 7)
|
|
|
|
|
|
|
|
|
|
switch(this.labelPosition) {
|
|
|
|
|
case 'left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, xpos-textSize.width-5, textSize.height+5)
|
|
|
|
|
break;
|
|
|
|
|
case 'right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, xpos+5, textSize.height+5)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 18:14:37 +00:00
|
|
|
|
if(this.targetValuePosition == 'Next to target' && this.getTargetElement() != null) {
|
2020-12-24 15:07:27 +00:00
|
|
|
|
var text = this.getTargetValueLabel()
|
|
|
|
|
var textSize = canvas.measureText(ctx, text, 7)
|
2020-12-24 18:14:37 +00:00
|
|
|
|
var ypox = canvas.y2px(this.getTargetElement().execute(this.x.execute()))
|
2020-12-24 15:07:27 +00:00
|
|
|
|
switch(this.labelPosition) {
|
|
|
|
|
case 'left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, xpos-textSize.width-5, ypox+textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'right':
|
2020-12-24 18:14:37 +00:00
|
|
|
|
canvas.drawVisibleText(ctx, text, xpos+5, ypox+textSize.height)
|
2020-12-24 15:07:27 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-22 23:23:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 23:59:57 +00:00
|
|
|
|
class Sequence extends ExecutableObject {
|
|
|
|
|
static type(){return 'Sequence'}
|
|
|
|
|
static typeMultiple(){return 'Sequences'}
|
|
|
|
|
static properties() {return {
|
2020-12-26 18:16:42 +00:00
|
|
|
|
'drawPoints': 'Boolean',
|
|
|
|
|
'drawDashedLines': 'Boolean',
|
|
|
|
|
'defaultExpression': new P.Dictionary('string', 'int', /^.+$/, /^\d+$/, '{name}[n+', '] = ', true),
|
2020-12-26 13:49:48 +00:00
|
|
|
|
'comment1': 'Note: Use {name}[n] to refer to {name}ₙ, {name}[n+1] for {name}ₙ₊₁...',
|
2020-12-26 18:16:42 +00:00
|
|
|
|
'baseValues': new P.Dictionary('string', 'int', /^.+$/, /^\d+$/, '{name}[', '] = '),
|
2021-01-05 10:25:58 +00:00
|
|
|
|
'labelPosition': new P.Enum('above', 'below', 'left', 'right', 'above-left', 'above-right', 'below-left', 'below-right'),
|
|
|
|
|
'labelX': 'number'
|
2020-12-25 23:59:57 +00:00
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
constructor(name = null, visible = true, color = null, labelContent = 'name + value',
|
2020-12-26 18:16:42 +00:00
|
|
|
|
drawPoints = true, drawDashedLines = true, defaultExp = {1: "n"},
|
2021-01-05 10:25:58 +00:00
|
|
|
|
baseValues = {0: 0}, labelPosition = 'above', labelX = 1) {
|
2020-12-25 23:59:57 +00:00
|
|
|
|
if(name == null) name = getNewName('uvwPSUVWabcde')
|
|
|
|
|
super(name, visible, color, labelContent)
|
2020-12-26 18:16:42 +00:00
|
|
|
|
this.drawPoints = drawPoints
|
|
|
|
|
this.drawDashedLines = drawDashedLines
|
2020-12-25 23:59:57 +00:00
|
|
|
|
this.defaultExpression = defaultExp
|
2020-12-26 18:16:42 +00:00
|
|
|
|
this.baseValues = baseValues
|
2021-01-05 10:25:58 +00:00
|
|
|
|
this.labelPosition = labelPosition
|
|
|
|
|
this.labelX = labelX
|
2020-12-26 18:16:42 +00:00
|
|
|
|
this.update()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export() {
|
|
|
|
|
return [this.name, this.visible, this.color.toString(), this.labelContent,
|
2021-01-05 10:25:58 +00:00
|
|
|
|
this.drawPoints, this.drawDashedLines, this.defaultExpression, this.baseValues, this.labelPosition, this.labelX]
|
2020-12-26 18:16:42 +00:00
|
|
|
|
}
|
2021-01-05 10:25:58 +00:00
|
|
|
|
|
2020-12-26 18:16:42 +00:00
|
|
|
|
update() {
|
|
|
|
|
super.update()
|
|
|
|
|
if(
|
|
|
|
|
this.sequence == null || this.baseValues != this.sequence.baseValues ||
|
|
|
|
|
this.sequence.name != this.name ||
|
|
|
|
|
this.sequence.expr != Object.values(this.defaultExpression)[0] ||
|
|
|
|
|
this.sequence.valuePlus != Object.keys(this.defaultExpression)[0]
|
|
|
|
|
)
|
|
|
|
|
this.sequence = new MathLib.Sequence(
|
|
|
|
|
this.name, this.baseValues,
|
|
|
|
|
Object.keys(this.defaultExpression)[0],
|
|
|
|
|
Object.values(this.defaultExpression)[0]
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getReadableString() {
|
|
|
|
|
return this.sequence.toString()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
execute(x = 1) {
|
|
|
|
|
if(x % 1 == 0)
|
|
|
|
|
return this.sequence.execute(x)
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
canExecute(x = 1) {return x%1 == 0}
|
|
|
|
|
// Simplify returns the simplified string of the expression.
|
|
|
|
|
simplify(x = 1) {
|
|
|
|
|
if(x % 1 == 0)
|
|
|
|
|
return this.sequence.simplify(x)
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-05 10:25:58 +00:00
|
|
|
|
getLabel() {
|
|
|
|
|
switch(this.labelContent) {
|
|
|
|
|
case 'name':
|
|
|
|
|
return `(${this.name}ₙ)`
|
|
|
|
|
case 'name + value':
|
|
|
|
|
return this.getReadableString()
|
|
|
|
|
case 'null':
|
|
|
|
|
return ''
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-26 18:16:42 +00:00
|
|
|
|
draw(canvas, ctx) {
|
|
|
|
|
Function.drawFunction(canvas, ctx, this.sequence, canvas.logscalex ? MathLib.Domain.NE : MathLib.Domain.N, MathLib.Domain.R, this.drawPoints, this.drawDashedLines)
|
2021-01-05 10:25:58 +00:00
|
|
|
|
|
|
|
|
|
// Label
|
|
|
|
|
var text = this.getLabel()
|
|
|
|
|
ctx.font = "14px sans-serif"
|
|
|
|
|
var textSize = canvas.measureText(ctx, text, 7)
|
|
|
|
|
var posX = canvas.x2px(this.labelX)
|
|
|
|
|
var posY = canvas.y2px(this.execute(this.labelX))
|
|
|
|
|
switch(this.labelPosition) {
|
|
|
|
|
case 'above':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width/2, posY-textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'below':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width/2, posY+textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY-textSize.height/2)
|
|
|
|
|
break;
|
|
|
|
|
case 'right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX, posY-textSize.height/2)
|
|
|
|
|
break;
|
|
|
|
|
case 'above-left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY-textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'above-right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX, posY-textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'below-left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY+textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'below-right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX, posY+textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-12-25 23:59:57 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-25 19:42:13 +00:00
|
|
|
|
|
2021-03-08 22:12:10 +00:00
|
|
|
|
class RepartitionFunction extends ExecutableObject {
|
2021-03-09 14:01:03 +00:00
|
|
|
|
static type(){return 'Repartition'}
|
|
|
|
|
static typeMultiple(){return 'Repartitions'}
|
2021-03-08 22:12:10 +00:00
|
|
|
|
static properties() {return {
|
|
|
|
|
'beginIncluded': 'Boolean',
|
|
|
|
|
'drawLineEnds': 'Boolean',
|
|
|
|
|
'comment1': 'Note: Specify the properties for each potential result.',
|
2021-03-09 14:01:03 +00:00
|
|
|
|
'probabilities': new P.Dictionary('string', 'float', /^-?[\d.,]+$/, /^-?[\d\.,]+$/, 'P({name} = ', ') = '),
|
2021-03-08 22:12:10 +00:00
|
|
|
|
'labelPosition': new P.Enum('above', 'below', 'left', 'right', 'above-left', 'above-right', 'below-left', 'below-right'),
|
|
|
|
|
'labelX': 'number'
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
constructor(name = null, visible = true, color = null, labelContent = 'name + value',
|
|
|
|
|
beginIncluded = true, drawLineEnds = true, probabilities = {0: 0}, labelPosition = 'above', labelX = 1) {
|
|
|
|
|
if(name == null) name = getNewName('XYZUVW')
|
|
|
|
|
super(name, visible, color, labelContent)
|
|
|
|
|
this.beginIncluded = beginIncluded
|
|
|
|
|
this.drawLineEnds = drawLineEnds
|
|
|
|
|
this.probabilities = probabilities
|
|
|
|
|
this.labelPosition = labelPosition
|
|
|
|
|
this.labelX = labelX
|
|
|
|
|
this.update()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export() {
|
|
|
|
|
return [this.name, this.visible, this.color.toString(), this.labelContent,
|
|
|
|
|
this.beginIncluded, this.drawLineEnds, this.probabilities, this.labelPosition, this.labelX]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getReadableString() {
|
2021-03-09 10:14:12 +00:00
|
|
|
|
var keys = Object.keys(this.probabilities).sort((a,b) => a-b);
|
2021-03-08 22:12:10 +00:00
|
|
|
|
return `F_${this.name}(x) = P(${this.name} ≤ x)\n` + keys.map(idx => `P(${this.name}=${idx})=${this.probabilities[idx]}`).join("; ")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
execute(x = 1) {
|
|
|
|
|
var ret = 0;
|
2021-03-09 10:14:12 +00:00
|
|
|
|
Object.keys(this.probabilities).sort((a,b) => a-b).forEach(idx => {
|
2021-03-09 14:01:03 +00:00
|
|
|
|
if(x >= idx) ret += parseFloat(this.probabilities[idx].replace(/,/g, '.'))
|
2021-03-08 22:12:10 +00:00
|
|
|
|
})
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
canExecute(x = 1) {return true}
|
|
|
|
|
// Simplify returns the simplified string of the expression.
|
|
|
|
|
simplify(x = 1) {
|
|
|
|
|
return this.execute(x)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getLabel() {
|
|
|
|
|
switch(this.labelContent) {
|
|
|
|
|
case 'name':
|
|
|
|
|
return `P(${this.name} ≤ x)`
|
|
|
|
|
case 'name + value':
|
|
|
|
|
return this.getReadableString()
|
|
|
|
|
case 'null':
|
|
|
|
|
return ''
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
draw(canvas, ctx) {
|
|
|
|
|
var currentY = 0;
|
2021-03-09 08:10:04 +00:00
|
|
|
|
var keys = Object.keys(this.probabilities).map(idx => parseInt(idx)).sort((a,b) => a-b)
|
2021-03-08 22:12:10 +00:00
|
|
|
|
console.log("Keys", keys)
|
2021-03-09 14:01:03 +00:00
|
|
|
|
if(canvas.visible(keys[0],this.probabilities[keys[0]].replace(/,/g, '.'))) {
|
2021-03-08 22:12:10 +00:00
|
|
|
|
canvas.drawLine(ctx,
|
|
|
|
|
0,
|
|
|
|
|
canvas.y2px(0),
|
|
|
|
|
canvas.x2px(keys[0]),
|
|
|
|
|
canvas.y2px(0)
|
|
|
|
|
)
|
|
|
|
|
if(canvas.visible(keys[0],0)) {
|
|
|
|
|
ctx.beginPath();
|
|
|
|
|
ctx.arc(canvas.x2px(keys[0])+4,canvas.y2px(0), 4, Math.PI / 2, 3 * Math.PI / 2);
|
|
|
|
|
ctx.stroke();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(var i = 0; i < keys.length-1; i++) {
|
|
|
|
|
var idx = keys[i];
|
2021-03-09 14:01:03 +00:00
|
|
|
|
currentY += parseFloat(this.probabilities[idx].replace(/,/g, '.'));
|
2021-03-08 22:12:10 +00:00
|
|
|
|
if(canvas.visible(idx,currentY) || canvas.visible(keys[i+1],currentY)) {
|
|
|
|
|
console.log("Drawing", idx, Math.max(0,canvas.x2px(idx)), canvas.y2px(currentY), Math.min(canvas.canvasSize.width,canvas.x2px(keys[i+1])), canvas.y2px(currentY))
|
|
|
|
|
canvas.drawLine(ctx,
|
|
|
|
|
Math.max(0,canvas.x2px(idx)),
|
|
|
|
|
canvas.y2px(currentY),
|
|
|
|
|
Math.min(canvas.canvasSize.width,canvas.x2px(keys[i+1])),
|
|
|
|
|
canvas.y2px(currentY)
|
|
|
|
|
)
|
|
|
|
|
if(canvas.visible(idx,currentY)) {
|
|
|
|
|
ctx.beginPath();
|
|
|
|
|
ctx.arc(canvas.x2px(idx),canvas.y2px(currentY), 4, 0, 2 * Math.PI);
|
|
|
|
|
ctx.fill();
|
|
|
|
|
}
|
|
|
|
|
if(canvas.visible(keys[i+1],currentY)) {
|
|
|
|
|
ctx.beginPath();
|
|
|
|
|
ctx.arc(canvas.x2px(keys[i+1])+4,canvas.y2px(currentY), 4, Math.PI / 2, 3 * Math.PI / 2);
|
|
|
|
|
ctx.stroke();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-09 08:10:04 +00:00
|
|
|
|
if(canvas.visible(keys[keys.length-1],currentY+parseFloat(this.probabilities[keys[keys.length-1]]))) {
|
2021-03-08 22:12:10 +00:00
|
|
|
|
canvas.drawLine(ctx,
|
|
|
|
|
Math.max(0,canvas.x2px(keys[keys.length-1])),
|
2021-03-09 14:01:03 +00:00
|
|
|
|
canvas.y2px(currentY+parseFloat(this.probabilities[keys[keys.length-1]].replace(/,/g, '.'))),
|
2021-03-08 22:12:10 +00:00
|
|
|
|
canvas.canvasSize.width,
|
2021-03-09 14:01:03 +00:00
|
|
|
|
canvas.y2px(currentY+parseFloat(this.probabilities[keys[keys.length-1]].replace(/,/g, '.')))
|
2021-03-08 22:12:10 +00:00
|
|
|
|
)
|
|
|
|
|
ctx.beginPath();
|
2021-03-09 08:10:04 +00:00
|
|
|
|
ctx.arc(
|
|
|
|
|
canvas.x2px(keys[keys.length-1]),
|
2021-03-09 14:01:03 +00:00
|
|
|
|
canvas.y2px(currentY+parseFloat(this.probabilities[keys[keys.length-1]].replace(/,/g, '.'))),
|
2021-03-09 08:10:04 +00:00
|
|
|
|
4, 0, 2 * Math.PI);
|
2021-03-08 22:12:10 +00:00
|
|
|
|
ctx.fill();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Label
|
|
|
|
|
var text = this.getLabel()
|
|
|
|
|
ctx.font = "14px sans-serif"
|
|
|
|
|
var textSize = canvas.measureText(ctx, text, 7)
|
|
|
|
|
var posX = canvas.x2px(this.labelX)
|
|
|
|
|
var posY = canvas.y2px(this.execute(this.labelX))
|
|
|
|
|
switch(this.labelPosition) {
|
|
|
|
|
case 'above':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width/2, posY-textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'below':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width/2, posY+textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY-textSize.height/2)
|
|
|
|
|
break;
|
|
|
|
|
case 'right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX, posY-textSize.height/2)
|
|
|
|
|
break;
|
|
|
|
|
case 'above-left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY-textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'above-right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX, posY-textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'below-left':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX-textSize.width, posY+textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
case 'below-right':
|
|
|
|
|
canvas.drawVisibleText(ctx, text, posX, posY+textSize.height)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 15:47:48 +00:00
|
|
|
|
const types = {
|
2020-12-22 00:01:36 +00:00
|
|
|
|
'Point': Point,
|
2020-12-22 15:47:48 +00:00
|
|
|
|
'Function': Function,
|
|
|
|
|
'Gain Bode': GainBode,
|
2020-12-22 23:23:38 +00:00
|
|
|
|
'Somme gains Bode': SommeGainsBode,
|
2020-12-24 15:07:27 +00:00
|
|
|
|
'Phase Bode': PhaseBode,
|
2020-12-24 18:14:37 +00:00
|
|
|
|
'Somme phases Bode': SommePhasesBode,
|
2020-12-25 23:59:57 +00:00
|
|
|
|
'X Cursor': CursorX,
|
2021-03-08 22:12:10 +00:00
|
|
|
|
'Sequence': Sequence,
|
2021-03-09 14:01:03 +00:00
|
|
|
|
'Repartition': RepartitionFunction,
|
2020-12-22 00:01:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var currentObjects = {}
|
2020-12-22 15:47:48 +00:00
|
|
|
|
|
2020-12-23 23:28:06 +00:00
|
|
|
|
function getObjectByName(objName, objType = null) {
|
|
|
|
|
var objectTypes = Object.keys(currentObjects)
|
|
|
|
|
if(typeof objType == 'string') {
|
|
|
|
|
if(currentObjects[objType] == undefined) return null
|
|
|
|
|
objectTypes = [objType]
|
|
|
|
|
}
|
|
|
|
|
if(Array.isArray(objType)) objectTypes = objType
|
2020-12-22 15:47:48 +00:00
|
|
|
|
var retObj = null
|
2020-12-23 23:28:06 +00:00
|
|
|
|
objectTypes.forEach(function(objType){
|
|
|
|
|
if(currentObjects[objType] == undefined) return null
|
|
|
|
|
currentObjects[objType].forEach(function(obj){
|
|
|
|
|
if(obj.name == objName) retObj = obj
|
|
|
|
|
})
|
2020-12-22 15:47:48 +00:00
|
|
|
|
})
|
|
|
|
|
return retObj
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getObjectsName(objType) {
|
|
|
|
|
if(currentObjects[objType] == undefined) return []
|
2020-12-24 00:31:57 +00:00
|
|
|
|
return currentObjects[objType].map(obj => obj.name)
|
2020-12-22 15:47:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-25 22:42:31 +00:00
|
|
|
|
function createNewRegisteredObject(objType, args=[]) {
|
2020-12-22 15:47:48 +00:00
|
|
|
|
if(Object.keys(types).indexOf(objType) == -1) return null // Object type does not exist.
|
2020-12-25 22:42:31 +00:00
|
|
|
|
var newobj = new types[objType](...args)
|
2020-12-22 15:47:48 +00:00
|
|
|
|
if(Object.keys(currentObjects).indexOf(objType) == -1) {
|
|
|
|
|
currentObjects[objType] = []
|
|
|
|
|
}
|
|
|
|
|
currentObjects[objType].push(newobj)
|
|
|
|
|
return newobj
|
|
|
|
|
}
|