简体   繁体   English

如何使用 javascript/jquery 旋转由点组成的半圆

[英]How to rotate a semi-circle made of points with javascript/jquery

More of a math questions than javascript... I have this code to create a semi-circle out of smaller circles but I am not sure how to rotate it so that the semi circle starts at the top of the circle and not the side (rotate it 45º to the left).更多的是数学问题而不是javascript ...我有这段代码可以用较小的圆圈创建一个半圆圈但我不确定如何旋转它以便半圆圈从圆圈的顶部而不是侧面开始(向左旋转 45º)。

 var items = 9; for (var i = 0; i < items; i++) { var x = 200 + 50 * Math.cos(1.1 * Math.PI * i / items); var y = 100 + 50 * Math.sin(1.1 * Math.PI * i / items); $("body").append("<div class='point' style='left:" + x + "px;top:" + y + "px'></div>"); }
 .point { width: 8px; height: 8px; border-radius: 50%; background: white; border: 1px solid black; position: absolute; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Current Output , Desired Output当前输出期望输出

Thanks!谢谢!

I believe this is what you want:我相信这就是你想要的:

 var items = 8; for(var i = 0; i < items; i++) { var x = 200 + 50 * Math.cos(Math.PI * i / items); var y = 100 + 50 * Math.sin(Math.PI * i / items); $("#center").append("<div class='point' style='left:"+ x +"px;top:"+ y +"px'></div>"); }
 #location { width:200px; height:200px; /* border-radius:100px; */ background: black; position:relative; left:200px; top:100px; } #center { transform: rotateZ(-80deg); }.point { width:8px; height:8px; border-radius:50%; background: white; border: 1px solid black; position:absolute; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="center"></div>

Simply invert x/y values in the top/left:只需反转顶部/左侧的 x/y 值:

 var items = 9; for (var i = 0; i < items; i++) { var x = 100 + 50 * Math.cos(1.1 * Math.PI * i / items); var y = 200 + 50 * Math.sin(1.1 * Math.PI * i / items); $("body").append("<div class='point' style='left:" + y + "px;top:" + x + "px'></div>"); }
 .point { width: 8px; height: 8px; border-radius: 50%; background: white; border: 1px solid black; position: absolute; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

You can swap the sin and cos calls.您可以交换sincos调用。 Not related to your question, but maybe you like the more jQuery style of adding elements:与您的问题无关,但也许您更喜欢添加元素的 jQuery 风格:

 var items = 9; for (var i = 0; i < items; i++) { var x = 200 + 50 * Math.sin(1.1 * Math.PI * i / items); var y = 100 + 50 * Math.cos(1.1 * Math.PI * i / items); $("body").append($("<div>").addClass("point").css({left: x, top: y})); }
 .point { width: 8px; height: 8px; border-radius: 50%; background: white; border: 1px solid black; position: absolute; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM