Java examples for 2D Graphics:Line
The Chop box Anchor's location is found by calculating the intersection of a line drawn from the center point of a box to a reference point and that box.
/*-------------------------------------------------------------------------+ | | | Copyright 2005-2011 The ConQAT Project | | | | Licensed under the Apache License, Version 2.0 (the "License"); | | you may not use this file except in compliance with the License. | | You may obtain a copy of the License at | | | | http://www.apache.org/licenses/LICENSE-2.0 | | | | Unless required by applicable law or agreed to in writing, software | | distributed under the License is distributed on an "AS IS" BASIS, | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | See the License for the specific language governing permissions and | | limitations under the License. | +-------------------------------------------------------------------------*/ //package com.java2s; import java.awt.Point; import java.awt.Rectangle; public class Main { /**//from w w w. j av a2 s . c o m * The ChopboxAnchor's location is found by calculating the intersection of * a line drawn from the center point of a box to a reference point and that * box. Code borrowed from org.eclipse.draw2d.ChopboxAnchor. */ public static Point getChopboxAnchor(Rectangle box, Point referencePoint) { double baseX = box.getCenterX(); double baseY = box.getCenterY(); double refX = referencePoint.x; double refY = referencePoint.y; // This avoids divide-by-zero if (box.isEmpty() || (refX == baseX && refY == baseY)) { return new Point((int) refX, (int) refY); } double dx = refX - baseX; double dy = refY - baseY; // r.width, r.height, dx, and dy are guaranteed to be non-zero. double scale = 0.5 / Math.max(Math.abs(dx) / box.width, Math.abs(dy) / box.height); baseX += dx * scale; baseY += dy * scale; return new Point((int) Math.round(baseX), (int) Math.round(baseY)); } }