Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculate x, y coordinates of rotated line segments to draw on a Canvas
    primarykey
    data
    text
    <p>This is less an HTML, CANVAS question and more of a general math question. I posted it here because it's prototyped using CANVAS and is still kind of a general programming question that I thought someone could answer. Here is the basic idea: I want to draw a line 10 pixels thick, but I don't want to use the standard lineTo and set a stroke width. I want to actually draw the border of the line using beginPath and lineTo. The reason being is this is actually for AS3 project and using this method allows us to have a line stroke and fill. So rotating the canvas and things of that nature are out of the question. I just need to figure out how to calculate the proper x, y coordinates for the line.</p> <p>In the code below is the coordinates for the top of a line. I basically want to take this coordinates, add 10 to the y axis for each one and that will give me the return coordinates for the bottom of the line. Of course, each segment of the line is rotated, so calculating the coordinates for the bottom of the line has proved tricky. I'm hoping someone can help.</p> <p>Once you run the example code, the issue should be obvious. The line isn't drawn properly. For relatively mild rotations of line segments it seems to work, but as the angle of rotation gets more severe the x, y coordinates are calculated incorrectly.</p> <pre><code>&lt;!doctype html&gt; &lt;html&gt; &lt;body&gt; &lt;canvas id="canvas" width="800" height="600"&gt; &lt;/canvas&gt; &lt;script type="text/javascript"&gt; var coords = [ {x:78,y:183}, {x:130,y:183}, {x:237,y:212}, {x:450,y:213}, {x:517,y:25}, {x:664,y:212}, {x:716,y:212} ]; var coordsBck = []; for( i = 0; i &lt; coords.length; i++ ) { var c1, c2, r; c1 = coords[i]; if( i &lt; coords.length - 1 ) { c2 = coords[i + 1]; r = Math.atan2((c2.y - c1.y),(c2.x - c1.x)); console.log( c1.x, c1.y, c2.x, c2.y, ( r * 180/Math.PI )); } else { r = 00; } var d = r * 180/Math.PI; var cos = Math.cos( r ); var sin = Math.sin( r ); var x = cos * 0 - sin * 10; var y = sin * 0 + cos * 10; coordsBck.push({x: c1.x + x, y: c1.y + y}); } while(coordsBck.length &gt; 0 ) { coords.push( coordsBck.pop() ); } var ctx = document.getElementById("canvas").getContext("2d"); ctx.beginPath(); for( i = 0; i &lt; coords.length; i++ ) { var line = coords[i]; console.log( i, line.x, line.y ); if( i == 0 ) { ctx.moveTo( line.x, line.y ); } else { ctx.lineTo( line.x, line.y ); } } ctx.fill(); function t(o) { return "x: " + o.x + ", y: " + o.y; } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload