为什么algebra.js无法解决y问题

时间:2018-05-21 14:06:22

标签: javascript algebra.js

在网站https://omegalords.ga/world/test.html上,我有一个代码,可以向目标计算1个单位。在代码中,斜率为1.因此跳转到的下一个点的x和y应该相同。为什么它会在某些时候发生变化而且会有2个坐标。我已经运行了数字,但它没有正常工作。有人可以解释一下吗?          

</canvas>
<script>
    var canvas = document.getElementById("myCanvas");
    var ctx = canvas.getContext("2d");
    var findDis = function(x1,y1,x2,y2){
        //Finds distance rounded to the nearest 1000th
        return Math.trunc(((Math.sqrt((x1-x2) * (x1-x2) + (y1-y2) * (y1-y2)))*1000))/1000
    }
    var findIntersections = function(b,a,m,r){
        var Fraction = algebra.Fraction;
        var Expression = algebra.Expression;
        var Equation = algebra.Equation;
        var coords = {
            l:[],
            g:[]
        }
        //b^2 - 2bx + x^2 - 2bm^2x + b^2m^2 - r^2
        var sl = algebra.parse(Math.pow(b,2)+" - "+2*b+"x + x^2 + "+Math.pow(m,2)+"x^2 -" + 2 * b * Math.pow(m,2) + "* x + " + Math.pow(b,2) * Math.pow(m,2) + " - " + Math.pow(r,2));
        var sr = algebra.parse("0")
        var eq = new Equation(sl, sr);
        //Solves for x rounded to the nearest 1000
        coords.l.push(Math.trunc((eq.solveFor("x")[0]*1000))/1000);
        coords.g.push(Math.trunc((eq.solveFor("x")[1]*1000))/1000);
        //Solves for y
        coords.l.push(Math.trunc((coords.l[0] * m + a - (b * m))*1000)/1000);
        coords.g.push(Math.trunc((coords.g[0] * m + a - (b * m))*1000)/1000);
        return JSON.stringify(coords);
    }
    var findSlope = function(x1,y1,x2,y2){
        //Finds Slope rounded to the nearest 1000th
        return Math.trunc(((y1-y2)/(x1-x2))*1000)/1000;
    }
    var getRandomInt = function(min, max) {
        min = Math.ceil(min);
        max = Math.floor(max);
        return Math.floor(Math.random() * (max - min)) + min; 
    }
    var wolves = [];
    var deers = [];
    var deer = function(x,y){
        this.x = x;
        this.y = y;
        this.hp = 100;
        this.size = 10;
        this.hunger = 0*%;
        this.preyTypes = [grass];
        this.predatorTypes = [wolves];
        deers.push(this);
    }
    var wolf = function(x,y){
        this.x = x;
        this.y = y;
        this.hp = 100;
        this.size = 10;
        this.hunger = 0 * %;
        wolves.push(this);
        this.preyTypes = [deers]
    }
    wolf.prototype.move = function(){
        this.nearbyPredators = [];
        this.nearbyPrey = [];
        //finds all nearby prey
        for(var j = 0;j<this.preyTypes.length;j++){
            for(var i = 0;i < this.preyTypes[j].length;i++){
                if(findDis(this.preyTypes[j][i].x,this.preyTypes[j][i].y,this.x,this.y) < 40){
                    this.nearbyPrey.push(this.preyTypes[j][i]);
                }else{
                    console.log(findDis(this.preyTypes[j][i].x,this.preyTypes[j][i].y,this.x,this.y))
                }
            }
        }
        //If there are no nearby prey, move randomly
        if(this.nearbyPrey.length === 0){
            wolf.x += getRandomInt(-8,9)
            wolf.y -= getRandomInt(-9,8);
        //If there is only one nearby prey, move towards it
        }else if(this.nearbyPrey.length === 1){
            this.nearestPrey = this.nearbyPrey[0]
            var slope = findSlope(this.nearestPrey.x,this.nearestPrey.y,this.x,this.y);
            console.log(slope);
            var coords = JSON.parse(findIntersections(this.x,this.y,slope,1));
            console.log(coords);
            var ldis = findDis(this.x,this.y,coords.l[0],coords.l[1]);
            var gdis = findDis(this.x,this.y,coords.g[0],coords.g[1]);
            console.log(ldis,gdis)
            if(ldis > gdis){
                this.x = coords.l[0]
                this.y = coords.l[1]
                console.log(this.x,this.y)
            }else {
                this.x = coords.g[0]
                this.y = coords.g[1]
                console.log(this.x,this.y)
            }
        //If there is more than 1, find the nearest prey and move towards it
        }else {
            for(var i = 0; i < this.nearbyPrey.length; i++){
                this.tempDis = getDis(this.x,this.y,this.nearbyPrey[i].x,this.nearbyPrey[i].y)
                if(typeof this.nearestPrey == 'undefined'){
                    this.nearestPrey = {
                        prey:this.nearbyPrey[i],
                        distance:this.tempDis
                    }
                    this.tempDis = undefined
                }else {
                    if(this.tempDis > this.nearestPrey.distance){
                        this.nearestPrey = {
                            prey:this.nearbyPrey[i],
                            distance:this.tempDis
                        }
                    }
                }
            }
            var slope = findSlope(this.nearestPrey.x,this.nearestPrey.y,this.x,this.y)
            var coords = JSON.parse(findIntersections(this.nearestPrey.x,this.nearestPrey.y,slope,1));
            var ldis = findDis(this.nearestPrey.x,this.nearestPrey.y,coords.l[0],coords.l[1]);
            var gdis = findDis(this.nearestPrey.x,this.nearestPrey.y,coords.g[0],coords.g[1]);
            if(ldis > gdis){
                this.x = coords.l[0]
                this.y = coords.l[1]
            }else {
                this.x = coords.g[0]
                this.y = coords.g[1]
            }
        }
    }
    wolf.prototype.bite = function(prey){
        prey.hp -= this.strength;
        this.hunger += 40 * %
        console.log(this.hunger);
        console.log(this.nearestPrey.hp)
    }
    new wolf(100,100)
    new deer(80,80);
    var init = function(){
        ctx.translate(0.5, 0.5);
        ctx.lineWidth = .5
        window.setInterval(draw,1000);
    }
    var draw = function(){
        ctx.clearRect(0,0,canvas.width,canvas.height)
        wolves[0].move();
        for(var i=0;i<wolves.length;i++){
            ctx.beginPath();
            ctx.arc(deers[0].x,deers[0].y,10,0,2*Math.PI);
            ctx.stroke();
            ctx.beginPath()
            ctx.arc(wolves[i].x,wolves[i].y,10,0,2*Math.PI);
            ctx.stroke();
        }
    }
    init()
</script>

0 个答案:

没有答案