作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个代码,如果它在 y=x 线上方,则将点分类为 1,如果在 y=x 线下方,则分类为 -1。我在 y=x 的 Canvas 中画线(因为 y 轴在 Canvas 中倒置,所以看起来像 y=-x)。然后我画每个点,如果它是 1 我把它画成绿色,如果它是 -1 我把它画成红色。我希望看到一条直线,一侧是绿色,一侧是红色。我运行了代码,得到了一个奇怪的结果。
这是我如何标记我的点的代码:
function Point(x, y){
this.x = x;
this.y = y;
this.label = 0;
if(this.y >= this.x){
this.label = 1;
console.log(x, y, "UP");
}else if(this.y < this.x){
this.label = -1;
console.log(x, y, "Down");
}
}
这是绘制点的代码:
function draw(){
ctx.clearRect(0, 0, can1.width, can1.height);
ctx.strokeStyle = "yellow";
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(can1.width, can1.height);
ctx.stroke();
ctx.closePath();
for(var i = 0; i < points.length; i++){
var x = points[i].x;
var y = points[i].y;
if(points[i].label == 1){
var color = "Chartreuse";
}else{
var color = "red";
}
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(x, y, 5, 0, Math.PI * 2, false);
ctx.fill();
ctx.closePath();
}
}
它似乎有点工作,但不完全?
编辑:x 和 y 值在代码的其他部分随机分配。
请帮忙,谢谢!
最佳答案
您可以将黄线绘制到所需的 x === y
而不仅仅是 Canvas 的末尾。
ctx.lineTo(Math.min(can1.width, can1.height), Math.min(can1.width, can1.height));
关于javascript - If 语句不起作用 (Javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48850441/
我是一名优秀的程序员,十分优秀!