Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing point on circle

I have a circle I created:

<div class='ring'></div>

.ring {
    border-radius: 50%;
    border: 2px solid;
  }

I need to draw points on the arc (like point A) and points inside the circle (like point B), how can I do it wisely? meaning that I will have the option to understand and adding points wherever I want to on the circle.

enter image description here

like image 697
Dor Cohen Avatar asked Sep 20 '15 16:09

Dor Cohen


People also ask

What is point on the circle?

A circle is a set of all points in a plane that are all an equal distance from a single point, the center. The distance from a circle's center to a point on the circle is called the radius of the circle. A radius is a line segment with one endpoint at the center of the circle and the other endpoint on the circle.

How do you find a point on a circle?

Then we can use the radius to find the circle's leftmost and rightmost points. The equation of a circle in standard form is (x−h)2+(y−k)2=r2, where (h,k) is the center, and r is the radius of the circle. The ordered pair (x,y) represents any point on the circle.

How do you show a point lies outside a circle?

To determine the position of a given point with respect to a circle, all we need to do is to find the distance between the point and the center of the circle, and compare it with the circle's radius. If the distance is greater than the radius, the point lies outside.


2 Answers

You won't be able to do that using CSS. The easiest way is to use HTML5 Canvas. Here is an example with some functions I created:

HTML

<canvas id="myCanvas" width="300" height="300">Your browser does not support the HTML5 canvas tag.</canvas>

JS

//Define Variables
var radius = 80;
var point_size = 4;
var center_x = 150;
var center_y = 150;
var font_size = "20px";

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

function drawCircle(){
    ctx.beginPath();
    ctx.arc(center_x, center_y, radius, 0, 2 * Math.PI);
    ctx.stroke();
}

function drawPoint(angle,distance,label){
    var x = center_x + radius * Math.cos(-angle*Math.PI/180) * distance;
    var y = center_y + radius * Math.sin(-angle*Math.PI/180) * distance;

    ctx.beginPath();
    ctx.arc(x, y, point_size, 0, 2 * Math.PI);
    ctx.fill();

    ctx.font = font_size;
    ctx.fillText(label,x + 10,y);
}

//Execution
drawCircle();
drawPoint(0,1,"A");
drawPoint(90,1.5,"B");
drawPoint(180,1,"C");
drawPoint(45,0.5,"D");

This will generate the folowing output:

enter image description here

Take a look how the angle and distance variables of the drawPoint function controls where the point will be located.

like image 53
Alvaro Flaño Larrondo Avatar answered Sep 20 '22 21:09

Alvaro Flaño Larrondo


you can add points using a simple math functions. You just must to know radius of this circle and angle (when you want to put circle). And then just use sin and cos to find out position of point.

for example to put point on circle edge on 45 degree:

x = radius + radius * sin(45);
y = radius + radius * cos(45);

So now you can use this position to create the dots.

like image 39
Illia Moroz Avatar answered Sep 22 '22 21:09

Illia Moroz