在SVG上绘制圆的线段

鲁本

给出以下信息:

  • 白点是圆的中心。
  • 蓝色,绿色点是圆边界中的点。
  • 方向:顺时针,逆时针

将任意点(蓝色,绿色)与白色点一起使用,那么我可以获得半径,并且可以绘制一个圆。但是我不想画一个圆,而只是画一个圆在蓝点和绿点之间的弧线

使用带有SVG(椭圆)的弧,我得到了large_arc_flag的2个选项

在此处输入图片说明

在此处输入图片说明

在此示例中:第二个选项是我想要的选项:large_arc_flag = 1,但有时我希望large_arc_flag = 0。实际上,我只想要属于该圆的弧。使用带有“ A”的路径,由于椭圆的相交,我有2条弧线可供选择。

How can I solve that?

Thanks!

Andrew Willems

Given a random circle centre (the white point), one random edge point (green) and a second mirrored edge point (blue), you can calculate the svg arc as follows.

  • Set the horizontal and vertical ellipse radii to the distance between the green and white points
  • Set the x-axis-rotation to zero (as rotating a circle makes no visual difference)
  • Set the large-arc-flag to one if the green point is above the white point, otherwise zero (I think this was the key thing you were asking)
  • Set the sweep flag to zero because the yellow path is arbitrarily being drawn from the left image border to the right image border, requiring counter-clockwise rotation of the arc path trajectory
  • Set the final point to the blue point coordinates

根据随机的中心点和边缘点,针对不同的弧形配置重复运行以下代码片段。

var xmlns = "http://www.w3.org/2000/svg"; // svg namespace
var doc = document; // common abbreviation
var spc = " "; // space
var com = ","; // comma

var wd = 200; // svg width
var ht = 200; // svg height
var svg = doc.querySelector("svg"); // retrieve svg root element
setAttributes(svg, {width: wd, height: ht}); // set the svg dimensions
var cenX = wd / 2; // centre the circle horizontally
var cenY = Math.random() * (ht / 2) + (ht / 4); // pick a random circle centre height

var x1 = Math.random() * (wd / 3) + (wd / 7); // pick a random green point position
var y1 = Math.random() * (ht / 2) + (ht / 4);

var x2 = wd - x1; // mirror the blue point on the green point
var y2 = y1;

showPt(cenX, cenY, "white"); // show the white circle centre
showPt(x1, y1, "green"); // show the coloured edge points
showPt(x2, y2, "blue");

var path = doc.createElementNS(xmlns, "path"); // create the yellow path element
setAttributes(path, { // give it colour and width but no fill
  stroke: "yellow",
  "stroke-width": 3,
  fill: "none"
});
svg.appendChild(path); // add it to the picture

var rad = Math.sqrt((x1-cenX)*(x1-cenX) + (y1-cenY)*(y1-cenY)); // calculate the circle radius
var lgArcFlag = (y1 < cenY ? 1 : 0); // the arc will be large if the edge points are above the circle centre
setAttributes(path, { // create the trajectory of the yellow path
  d:
    "M" +
    "0," + y1 + // start it at the left border at the height of the green edge point
    "L" +
    x1 + com + y1 + spc + // draw it to the green edge point
    "A" + // make an arc
    rad + spc + rad + spc + // using the circle radius
    0 + spc + // with no rotation of the ellipse/circle
    lgArcFlag + spc + 0 + spc +  // using the large-arc-flag
    x2 + com + y2 + spc + // drawn to the blue edge point
    "L" +
    wd + com + y1 // and drawn straight out to the right border
});

function showPt(x, y, fill) {
  var pt = doc.createElementNS(xmlns, "circle");
  setAttributes(pt, {
    cx: x,
    cy: y,
    r: 8,
    fill: fill
  });
  svg.appendChild(pt);
  return pt;
}

function setAttributes(el, attrs) {
  var recursiveSet = function(at, set) {
    for (var prop in at) {
      var a = at[prop];
      if (typeof a === 'object' && a.dataset === undefined && a[0] === undefined) {
        recursiveSet(a, set [prop]);
      } else {
        set.setAttribute(prop, a);
      }
    }
  }
  recursiveSet(attrs, el);
}
<svg>
  <rect id="bkgd" fill="black" x="0" y="0" width="300" height="300" />
</svg>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在mouseclick上的画布上绘制实心圆

来自分类Dev

在SVG元素中检测圆上的touchmove

来自分类Dev

如何在画布上绘制具有径向渐变的圆?

来自分类Dev

在JPanel上绘制SVG图像

来自分类Dev

在Android画布上绘制空心圆

来自分类Dev

Java在Swing上绘制圆和线

来自分类Dev

如何使用Matlab在视频帧上绘制实心圆

来自分类Dev

将线段叠加到圆上

来自分类Dev

libgdx在pixmap上绘制半透明圆

来自分类Dev

使用itextsharp C#在现有PDF上绘制圆

来自分类Dev

用已知坐标和半径在栅格上绘制圆

来自分类Dev

尝试使用d3在圆上绘制圆弧

来自分类Dev

如何在SVG中绘制带边框的圆?

来自分类Dev

计算圆上的SVG圆弧路径

来自分类Dev

绘制路径上的SVG动画

来自分类Dev

如何在内圆内外圆上删除线段?

来自分类Dev

如何绘制线段的轮廓

来自分类Dev

确定圆上的线段选择

来自分类Dev

SVG:在图像上绘制标记

来自分类Dev

在屏幕上绘制后,圆弧或圆不是完美的圆

来自分类Dev

在SVG中绘制圆的圆周部分

来自分类Dev

svg绘制带有弯曲文本的圆

来自分类Dev

在圆上绘制点

来自分类Dev

在圆上绘制均匀大小的标签

来自分类Dev

HTML5画布-绘制线段和圆-一个圆使用不同的颜色

来自分类Dev

无法在屏幕上绘制圆

来自分类Dev

在WPF上用GDI图形绘制圆

来自分类Dev

计算端点位于圆上的线段的交点数?

来自分类Dev

在图像上绘制真正的实心圆