I make a drawing, and for her an asterisk-shaped brush. The size of the asterisk is arbitrary, and the form itself is given in polar coordinates relative to the center. Accordingly, sine and cosine are used, returning non-integer numbers.
Question: when I try to draw something based on these coordinates, are these numbers rounded to integers or somehow taken into account?
The problem is that the edges of the lines look very clumsy and I suspect that the coordinates are rounded.
The code is repeated every time the addClick function receives new coordinates.
var shape; const buf = 0.6; // const buf = 1; shape = [0, 1, 36, buf, 72, 1, 108, buf, 144, 1, 180, buf, 216, 1, 252, buf, 288, 1, 324, buf]; // Star // shape = [135, 1, 315, 1]; // Slash // shape = [133, 1, 137, 1, 313, 1, 317, 01]; // Slash // shape = [45, 1, 225, 1]; // Backlash // Π‘ΠΌΠ΅ΡΠ°Π΅ΠΌ Π³ΡΠ°Π΄ΡΡΡ ΠΈΠ· ΡΠ΄ΠΎΠ±Π½ΡΡ
Π² ΡΠ΅Π°Π»ΡΠ½ΡΠ΅ ΠΈ ΠΏΠ΅ΡΠ΅Π²ΠΎΠ΄ΠΈΠΌ Π² ΡΠ°Π΄ΠΈΠ°Π½Ρ for (var j = 0; j < shape.length; j += 2) { shape[j] -= 90; shape[j] = shape[j] * Math.PI / 180; } // ΠΠ΅ΡΠ΅Π²ΠΎΠ΄ΠΈΠΌ ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΡ ΠΈΠ· ΡΠ°Π΄ΠΈΠ°Π»ΡΠ½ΡΡ
Π² Π΄Π΅ΠΊΠ°ΡΡΠΎΠ²Ρ Ρ ΡΡΡΡΠΎΠΌ ΡΠ°Π·ΠΌΠ΅ΡΠ° ΠΊΠΈΡΡΠΈ { var deg, rad; for (var j = 0; j < shape.length; j += 2) { deg = shape[j]; rad = shape[j + 1]; shape[j] = radius / 2 * rad * Math.cos(deg); shape[j + 1] = radius / 2 * rad * Math.sin(deg); } } // ΠΠ°Ρ
ΠΎΠ΄ΠΈΠΌ ΡΠ°ΠΌΡΡ ΠΎΡΠ΄Π°Π»ΡΠ½Π½ΡΡ ΡΠΎΡΠΊΡ Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΡΡΠΎΡΠΎΠ½Ρ var point1 = [0, 0]; // l, j var point2 = [0, 0]; var x, y, l; var k = (my - ly) / (mx - lx); for (var j = 0; j < shape.length; j += 2) // ΠΠ΅ΡΠ΅Π±ΠΈΡΠ°Π΅ΠΌ Π²ΡΠ΅ ΡΠΎΡΠΊΠΈ ΡΠ΅ΠΊΡΡΠ΅ΠΉ ΡΠΎΡΠΌΡ { x = shape[j]; y = shape[j + 1]; // ΠΠ°Ρ
ΠΎΠ΄ΠΈΠΌ ΡΠ°ΡΡΡΠΎΡΠ½ΠΈΠ΅ ΠΎΡ ΡΠΎΡΠΊΠΈ Π΄ΠΎ ΠΏΡΡΠΌΠΎΠΉ if (lx == mx) // ΠΡΠ»ΠΈ ΠΏΡΡΠΌΠ°Ρ Π²Π΅ΡΡΠΈΠΊΠ°Π»ΡΠ½Π° { l = Math.abs(x); if (x < 0 && l > point1[0]) { point1[0] = l; // Π Π°ΡΡΡΠΎΡΠ½ΠΈΠ΅ Π΄ΠΎ ΠΏΡΡΠΌΠΎΠΉ point1[1] = j; // ΠΠΎΠΌΠ΅Ρ ΡΠΎΡΠΊΠΈ } if (x > 0 && l > point2[0]) { point2[0] = l; point2[1] = j; } } else { l = Math.abs(k*x - y) / Math.sqrt(k**2 + 1); if (y < 0 && l > point1[0]) { point1[0] = l; point1[1] = j; } if (y > 0 && l > point2[0]) { point2[0] = l; point2[1] = j; } } } // ΠΡΡΠ°Π²Π»ΡΠ΅ΠΌ ΡΠΎΠ»ΡΠΊΠΎ Π½ΠΎΠΌΠ΅ΡΠ° ΡΠΎΡΠ΅ΠΊ point1 = point1[1]; point2 = point2[1]; // ΠΡΠ±ΠΈΡΠ°Π΅ΠΌ, Ρ ΠΊΠ°ΠΊΠΎΠΉ Π½Π°ΡΠ°ΡΡ if (lx < mx || lx == mx && ly > my) // ΠΡΠ»ΠΈ Π»ΠΈΠ½ΠΈΡ Π½Π°ΠΏΡΠ°Π²Π»Π΅Π½Π° Π²Π»Π΅Π²ΠΎ ΠΈΠ»ΠΈ Π²Π΅ΡΡΠΈΠΊΠ°Π»ΡΠ½ΠΎ Π²Π½ΠΈΠ· { // ΠΠ΅Π½ΡΠ΅ΠΌ ΡΠΎΡΠΊΠΈ ΠΌΠ΅ΡΡΠ°ΠΌΠΈ point1 = point1 + point2; point2 = point1 - point2; point1 = point1 - point2; } // ΠΠΎΡΡΡΠΎΠΈΡΡ Π»ΠΈΠ½ΠΈΡ ΠΏΠΎ ΡΠΎΡΠΌΠ΅ context.beginPath(); context.moveTo(mx + shape[point1], my + shape[point1 + 1]); context.lineTo(lx + shape[point1], ly + shape[point1 + 1]); var j = (point1 + 2) % shape.length; x = lx; y = ly; while (true) { context.lineTo(x + shape[j], y + shape[j + 1]); if (j == point2) { x = mx; y = my; context.lineTo(x + shape[j], y + shape[j + 1]); } j += 2; if (j >= shape.length) j = j % shape.length; if (j == point1) break; } context.closePath(); context.fillStyle = gradient; // context.fillStyle = colorGreen; context.fill(); 