js求两圆交点_Chart.js找到交点Point并绘制一个圆

I am using chart.js for generating line chart. Its works me successfully. But I need to draw a circle or point at Intersection and draw a line towards "X" axis. Can you please any one help me to get this?

Thanks in advance.

解决方案

The main points to achieve this with chartjs (without being an chartjs expert by any means) would be:

Method 1 - Math

Step 1

Unless you can get a processed point array from chartjs' internals, you would have to, as chartjs uses Bezier curves to draw the graphs, manually convert your point data set into a new point array representing the drawn line.

You would also have to consider scale the same way as chartjs does. As canvas does not provide the points for its Bezier method, you will have to calculate them

Have in mind though: you can not just use some random control points - you will have to replicate these too for the curve the same way chartjs does, so you need to use

Step 2

When both lines are in "Bezier form" you will have to limit the range you want to search by finding which segments of the lines are covering the range you want to check them against.

Do to this using line.x1 <= range.x1 <= line.x2 and the same for range.x2 of the range you want to search (the y axis is not important in this step).

You should end up having two arrays with line segments that matches the range.

(of course, if you don't need to reuse the curves and only need a single segment, you can just find the segments in step 1 and use that for this step).

Step 3

Now you need to loop through array one.

For the current segment in array one, you need to test against all the segments in array two doing intersection test using a

Final

Now you can extract the intersecting point (if any) and plot it to chartjs' canvas (and congratulation, you are also halfway to your own chart widget :P ).

Method 2 - Brute Force

Step 1

Get the bitmap of the canvas.

Step 2

Define a range you want to search for intersection

Calculate the composed color where the two lines meet. This will be the color you search for (you can do a pre-step instead, locating a known intersection and read the pixel value from that point).

Step 3

Scan line by line (vertically) and test each pixel for color values. You need to use a tolerance range (+/- t%) as the canvas pixels are integer values while your result from mixing is a floating point value.

Final

When a pixel has been found add a delta to the value to compensate for line width. Plot to canvas.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值