Draw an arrow between two circles?

后端 未结 4 1063
再見小時候
再見小時候 2021-02-10 07:31

How can I draw an arrowed line between two circles, given:

  1. Location of the centers of the cirlces
  2. Radius of the circles

I am using

4条回答
  •  耶瑟儿~
    2021-02-10 08:09

    As @andsens said, you're doing a simple vector manipulation.

    This can be done much more cleanly if you wrap it in a decent library. For example, I use the nice Sylvester matrix and vector library.

    What you're essentially calculating is:

    V subscript edge equals open paren modulus V minus R close paren times unit vector V

    Where v is the vector to the centre of the target, and vedge the vector to the edge of the target with radius r.

    Which you can do easily:

    // Assume source and target both have x and y properties
    // Assume target has radius property
    function path2TargetEdge(source, target){
    
      // V is the vector from the source to the target's center
      var V = $V([target.x-source.x, target.y-source.y]);
    
      // Vt is the vector from the source to the edge of the target
      var Vt = V.toUnitVector().multiply(V.modulus() - target.radius);
    
      return {x: Vt.e(1), y: Vt.e(2) }; // Vectors are 1-indexed
    }
    

提交回复
热议问题