Adobe Flex 3 を使用して作図ツールを構築しています。コネクタ ラインを実装しようとしていますが、質問があります。
キャンバス上のランダムな位置に 2 つの正方形があるとします。それらの間に矢印付きのコネクタ ラインを描画する必要があります。ターゲットの正方形の中心に向かう傾向がありますが、その境界で終わる必要があります。
線を引く正確なポイントを見つけるにはどうすればよいですか?
ありがとうございました
これがあなたがやりたいことをする例です。
package
{
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.ui.Mouse;
/**
* Sample class to draw squares and arrows between them.
*/
public class SquareArrows extends Sprite
{
/**
* Initialize the scene as soon as we can.
*/
public function SquareArrows()
{
if(stage) {
init();
}
else {
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
/**
* Draw two squares and an arrow between them.
*/
private function init(e : Event = null) : void
{
if(hasEventListener(Event.ADDED_TO_STAGE)) {
removeEventListener(Event.ADDED_TO_STAGE, init);
}
// Drawing random-sized squares.
var squareOne : Shape =
getSquareShape((Math.random() * 50) + 20, 0xBBBBBB);
var squareTwo : Shape =
getSquareShape((Math.random() * 50) + 20, 0xDDDDDD);
addChild(squareOne);
addChild(squareTwo);
// Draw the connector.
var connector : Shape = getConnectorShape(squareOne, squareTwo);
addChild(connector);
}
/**
* Draw a connector arrow between two square shapes.
*/
private function getConnectorShape(connectFrom : Shape, connectTo : Shape) : Shape
{
// Getting the center of the first square.
var centerFrom : Point = new Point();
centerFrom.x = connectFrom.x + (connectFrom.width / 2);
centerFrom.y = connectFrom.y + (connectFrom.height / 2);
// Getting the center of the second square.
var centerTo : Point = new Point();
centerTo.x = connectTo.x + (connectTo.width / 2);
centerTo.y = connectTo.y + (connectTo.height / 2);
// Getting the angle between those two.
var angleTo : Number =
Math.atan2(centerTo.x - centerFrom.x, centerTo.y - centerFrom.y);
var angleFrom : Number =
Math.atan2(centerFrom.x - centerTo.x, centerFrom.y - centerTo.y);
// Getting the points on both borders.
var pointFrom : Point = getSquareBorderPointAtAngle(connectFrom, angleTo);
var pointTo : Point = getSquareBorderPointAtAngle(connectTo, angleFrom);
// Calculating arrow edges.
var arrowSlope : Number = 30;
var arrowHeadLength : Number = 10;
var vector : Point =
new Point(-(pointTo.x - pointFrom.x), -(pointTo.y - pointFrom.y));
// First edge of the head...
var edgeOneMatrix : Matrix = new Matrix();
edgeOneMatrix.rotate(arrowSlope * Math.PI / 180);
var edgeOneVector : Point = edgeOneMatrix.transformPoint(vector);
edgeOneVector.normalize(arrowHeadLength);
var edgeOne : Point = new Point();
edgeOne.x = pointTo.x + edgeOneVector.x;
edgeOne.y = pointTo.y + edgeOneVector.y;
// And second edge of the head.
var edgeTwoMatrix : Matrix = new Matrix();
edgeTwoMatrix.rotate((0 - arrowSlope) * Math.PI / 180);
var edgeTwoVector : Point = edgeTwoMatrix.transformPoint(vector);
edgeTwoVector.normalize(arrowHeadLength);
var edgeTwo : Point = new Point();
edgeTwo.x = pointTo.x + edgeTwoVector.x;
edgeTwo.y = pointTo.y + edgeTwoVector.y;
// Drawing the arrow.
var arrow : Shape = new Shape();
with(arrow.graphics) {
lineStyle(2);
// Drawing the line.
moveTo(pointFrom.x, pointFrom.y);
lineTo(pointTo.x, pointTo.y);
// Drawing the arrow head.
lineTo(edgeOne.x, edgeOne.y);
moveTo(pointTo.x, pointTo.y);
lineTo(edgeTwo.x, edgeTwo.y);
}
return arrow;
}
/**
* Utility method to get a point on a square border at a certain angle.
*/
private function getSquareBorderPointAtAngle(square : Shape, angle : Number) : Point
{
// Calculating rays of inner and outer circles.
var minRay : Number = Math.SQRT2 * square.width / 2;
var maxRay : Number = square.width / 2;
// Calculating the weight of each rays depending on the angle.
var rayAtAngle : Number = ((maxRay - minRay) * Math.abs(Math.cos(angle * 2))) + minRay;
// We have our point.
var point : Point = new Point();
point.x = rayAtAngle * Math.sin(angle) + square.x + (square.width / 2);
point.y = rayAtAngle * Math.cos(angle) + square.y + (square.height / 2);
return point;
}
/**
* Utility method to draw a square of a given size in a new shape.
*/
private function getSquareShape(edgeSize : Number, fillColor : Number) : Shape
{
// Draw the square.
var square : Shape = new Shape();
with(square.graphics) {
lineStyle(1);
beginFill(fillColor);
drawRect(0, 0, edgeSize, edgeSize);
endFill();
}
// Set a random position.
square.x = Math.random() * (stage.stageWidth - square.width);
square.y = Math.random() * (stage.stageHeight - square.height);
return square;
}
}
}
このコードは完全に最適化されているわけではありません。アイデアは、それがどのように機能するかを説明することです。基本的に、2つの(ランダムな)正方形を定義し、それらの間の線をトレースします。線をトレースするには、最初の正方形の中心から2番目の正方形の中心までの角度を計算し、特別な方法(getSquareBorderPointAtAngle
)を使用して、正方形の境界上の点を正しい方向に抽出します。
このメソッドは、このスニペットの最初の重要なポイントです。単純な円のジオメトリを使用して計算しますが、正方形の周囲または内側の円を一致させるのではなく、点を境界に一致させる方法を少し複雑にします。
次に、矢じりを描きます。そのために、FlashMatrix
クラスを使用しています。これは、最初から計算するよりもこの方法の方がはるかに簡単だからです。
これで完了です。
同じことが必要なので、1か月前にここで回答を読んでいました。その間、このコネクタの描画例を見つけて、リンクを共有したいと思いました。
この例では、UIComponents 間にコネクタ ラインを描画し、コネクタがドラッグされるとラインを更新します。良いですね!
(ソース: sammyjoeosborne.com )
http://sammyjoeosborne.com/Examples/Connector/ConnectorExample.html
最も簡単なのは、おそらく を使用することflash.geom.Point
です。と の両方の中心c1
を取りc2
ます。d
それらの差であるベクトルを取ります。その角度 (315 から 45、45 から 135、135 から 225、225 から 315) に応じて、関係する側面 (それぞれ、右と左、上と下、左と右、下と上) がわかります。
次に、各辺と中心を結ぶ線との交点を計算します。
中心を結ぶ線はp=t*v+c1
(ベクトルで言えば) として表すことができます。側を線として表し、t
両方の方程式が同じ点p
を生成するように計算します。これが探している交点です。