首页 前端知识 【绘制“函数图”——html实现】

【绘制“函数图”——html实现】

2024-06-09 09:06:17 前端知识 前端哥 400 137 我要收藏

return new Segment(Math.pow(v1, v2));

}

};

}

//Class for special functions

function MathFunction(input) {

this.f = input;

if (!input) {

console.log(“Math function has no input.”);

}

this.solve = function (segment) {

var v = segment.coefficient;

if (this.f == “sin”) {

return new Segment(Math.sin(v));

} else if (this.f == “cos”) {

return new Segment(Math.cos(v));

} else if (this.f == “tan”) {

return new Segment(Math.tan(v));

} else if (this.f == “asin”) {

return new Segment(Math.asin(v));

} else if (this.f == “acos”) {

return new Segment(Math.acos(v));

} else if (this.f == “atan”) {

return new Segment(Math.atan(v));

} else if (this.f == “abs”) {

return new Segment(Math.abs(v));

}

};

}

//Class for a segment of math (a container)

function Segment(input) {

this.sections = [];

this.type = “section”;

this.operator = 0;

this.coefficient = 0;

this.mathFunction = 0;

this.variable = “”;

var removeBrackets = function (value) {

if (!value) return “”;

//While there are brackets around the string

while (value.substr(0, 1) == “(” && value.substr(value.length - 1, 1) == “)”) {

var openBrackets = 1;

//See if the end bracket closes the opening bracket or not

for (var i = 1; i < value.length && openBrackets > 0; i ) {

if (value.substr(i, 1) == “(”) openBrackets ;

if (value.substr(i, 1) == “)”) openBrackets–;

}

i -= 1;

//If it corresponds to different brackets, do nothing

if (openBrackets !== 0 || i != value.length - 1) {

break;

//Otherwise, remove the brackets, continue loop to see if there are more

} else {

value = value.substring(1, value.length - 1);

}

}

return value;

};

var findLast = function (operator, value) {

//Keep searching for the next last sign if the one found is within brackets

var inBrackets = true;

var index = -1;

if (operator != “^”) {

index = value.lastIndexOf(operator);

} else {

index = value.indexOf(operator); //Look for the first instead of last if it’s an exponent

}

var operators = “±/*^”;

while (inBrackets) {

var openBrackets = 0;

//Find how many brackets are opened or closed at a given point in the string

for (var i = 0; i < value.length; i ) {

if (value.substr(i, 1) == “(”) {

openBrackets ;

} else if (value.substr(i, 1) == “)”) {

openBrackets–;

}

if (i == index) {

//If no brackets are open (and if the operator is actually - and not just a minus sign), break the loop.

if ((openBrackets === 0 && (operator != “-” || (i > 0 && operators.indexOf(value.substr(i - 1, 1)) == -1) || i === 0)) || (openBrackets == 1 && operator == “(”)) {

inBrackets = false;

break;

//Otherwise, find the next operator, and loop through again to see if that one is in brackets

} else {

if (operator != “^”) {

index = value.substring(0, index).lastIndexOf(operator);

} else {

var nextOperator = value.substring(index 1).indexOf(operator);

if (nextOperator == -1) {

index = -1;

} else {

index = (index 1 value.substring(index 1).indexOf(operator));

}

}

}

}

}

//If no more operators are found, break the loop

if (index == -1) {

inBrackets = false;

}

}

return index;

};

var findLastTrig = function (trig, value) {

var matches = 0;

var index = -1;

var r = 0;

if (trig == “sin”) {

r = /(a)?sin/g;

} else if (trig == “cos”) {

r = /(a)?cos/g;

} else if (trig == “tan”) {

r = /(a)?tan/g;

} else {

return -1;

}

for (matches = r.exec(value); matches; matches = r.exec(value)) if (RegExp.$1 != “a”) index = matches.index;

var inBrackets = true;

while (inBrackets && index != -1) {

var openBrackets = 0;

//Find how many brackets are opened or closed at a given point in the string

for (var i = 0; i < value.length; i ) {

if (value.substr(i, 1) == “(”) {

openBrackets ;

} else if (value.substr(i, 1) == “)”) {

openBrackets–;

}

if (i == index) {

//If no brackets are open (and if the operator is actually - and not just a minus sign), break the loop.

if (openBrackets === 0) {

inBrackets = false;

break;

//Otherwise, find the next operator, and loop through again to see if that one is in brackets

} else {

var sub = value.substring(0, index);

index = -1;

for (matches = r.exec(sub); matches; matches = r.exec(sub)) if (RegExp.$1 != “a”) index = matches.index;

转载请注明出处或者链接地址:https://www.qianduange.cn//article/11625.html
标签
评论
发布的文章

安装Nodejs后,npm无法使用

2024-11-30 11:11:38

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!