javascript - How to calculate and place circles on an rect area? -
I am trying to draw some charts on the canvas area. Following my problem ...
I have to draw 1-4 (or more) circles, the canvas size is like 500 x 400 px. How can I calculate the maximum radius of each circle to keep all the space on this canvas now and how can I get the position of each circle (center X / Y)? So can each circle be optimal in some margin area for each other?
Here are some example screens that show you what I mean ...
very much Thanks!
Maximum radius you can use
var numberOfSections = 4 Are; Var width = 500; Var Height = 400; Var R = Math.sqrt ((width * height) / numberoffEXaction) / 2 var MX = MathRed (width / (R * 2)); // Maximum amount of squares, which can fit on width MY = Math field (height / (R * 2)); // The maximum amount of squares that can fit at height is skipLlast = 0; Var numOfCalculatedCircles = MX * MY; If (numOfCalculatedCircles! = NumberOfSections) {if (numOfCalculatedCircles & lt; numberOfSections) {console.log ('numOfCalculatedCircles', numOfCalculatedCircles); MX = MX + Math CEC ((Number of Numbers - Number Acrylicized Circles) / My); If (MX * MY! = NumberOfSections) {skipLast = Math.abs (MX * MY-numberOfSections); }} And {skipLast = numOfCalculatedCircles - numberOfSections ;; } Console.log ('MX * MY', MX * ME); } // Radius recalculation for X (R * 2 * MX> width) {R = (width / 2) / MX; } // Re-calculate the radius for Y if (R * 2 * MY> gt; height) {R = (height / 2} / MY} For X and Y Calculate the margin:
preferences weld = R * 2 * mx; Var circles hi = R * 2 * MY; Var margin x = 0; Var margin Y = 0; If (circlewidth and width) {marginX = (width - circlesview) / 2} if (circalite lieutenance; height) {margin Y = (height - circle height) / 2} You can calculate the centers:
var RE = margin Y + R; Var radius padding = 10; (Var i = 0; i & lt; MY; i ++) for {var RX = margin x + r; (Var j = 0; j & lt; mx; j ++) {if (i === MY - 1) {if (j === MX - skipLast) {break; }} Canvas.drawArc ({center: true, strokeStyle: 'red', strokeWidth: 1, start: 0, end: 360, radius: R - radiating padding, x: rx, y: ry}}; rx + = 2 * R;} RI + = 2 * R; Update: This is still incomplete but it can work in this special example:
Comments
Post a Comment