~ubuntu-branches/ubuntu/trusty/cdk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/renderer/ArrowRenderer2D.java

  • Committer: Bazaar Package Importer
  • Author(s): Paul Cager
  • Date: 2008-04-09 21:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080409211753-46lmjw5z8mx5pd8d
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Revision: 7636 $ $Author: egonw $ $Date: 2007-01-04 18:46:10 +0100 (Thu, 04 Jan 2007) $
 
2
 *  
 
3
 * Copyright (C) 2006-2007  Christoph Steinbeck <steinbeck@users.sf.net>
 
4
 *
 
5
 * Contact: cdk-devel@lists.sourceforge.net
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Lesser General Public License
 
9
 * as published by the Free Software Foundation; either version 2.1
 
10
 * of the License, or (at your option) any later version.
 
11
 * All we ask is that proper credit is given for our work, which includes
 
12
 * - but is not limited to - adding the above copyright notice to the beginning
 
13
 * of your source code files, and to any copyright notice that you may distribute
 
14
 * with programs based on this work.
 
15
 *
 
16
 * This program is distributed in the hope that it will be useful,
 
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
 * GNU Lesser General Public License for more details.
 
20
 *
 
21
 * You should have received a copy of the GNU Lesser General Public License
 
22
 * along with this program; if not, write to the Free Software
 
23
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 
24
 */
 
25
package org.openscience.cdk.renderer;
 
26
 
 
27
import org.openscience.cdk.geometry.GeometryTools;
 
28
import org.openscience.cdk.interfaces.IAtom;
 
29
import org.openscience.cdk.interfaces.IAtomContainer;
 
30
import org.openscience.cdk.tools.LoggingTool;
 
31
 
 
32
import javax.vecmath.Point2d;
 
33
import javax.vecmath.Vector2d;
 
34
import java.awt.*;
 
35
import java.awt.geom.Arc2D;
 
36
import java.awt.geom.Ellipse2D;
 
37
 
 
38
/**
 
39
 * A Renderer class which draws 2D representations of curly arrows as used for
 
40
 * representing electron movements in organic chemistry text books.
 
41
 * Adapted from <a href="http://www.wam.umd.edu/~petersd/Interp2_code.java">http://www.wam.umd.edu/~petersd/Interp2_code.java</a>.
 
42
 * 
 
43
 * @author      steinbeck
 
44
 * @cdk.module  render
 
45
 * @cdk.created 2006-03-28
 
46
 * @cdk.keyword viewer, 2D-viewer
 
47
 * @see         org.openscience.cdk.renderer.Renderer2DModel
 
48
 */
 
49
public class ArrowRenderer2D {
 
50
 
 
51
        Renderer2DModel r2dm;
 
52
 
 
53
        protected LoggingTool logger;
 
54
        double rotAngle; 
 
55
        double offsetAngle;
 
56
        Point2d center; 
 
57
        Vector2d vector3;
 
58
 
 
59
        //private Vector points = new Vector(16, 4);
 
60
 
 
61
        //private int precision = 10;
 
62
        
 
63
 
 
64
        /**
 
65
         * Constructs a ArrowRenderer2D with a default settings model.
 
66
         */
 
67
        public ArrowRenderer2D() {
 
68
                this(new Renderer2DModel());
 
69
        }
 
70
 
 
71
        /**
 
72
         * Constructs a ArrowRenderer2D.
 
73
         * 
 
74
         * @param r2dm
 
75
         *            The settings model to use for renderingraphics.
 
76
         */
 
77
        public ArrowRenderer2D(Renderer2DModel r2dm) {
 
78
                this.r2dm = r2dm;
 
79
                logger = new LoggingTool(this);
 
80
 
 
81
        }
 
82
 
 
83
        /**
 
84
         */
 
85
        public void paintArrows(Arrow[] arrows, Graphics2D graphics) 
 
86
        {
 
87
                
 
88
                for (int f = 0; f < arrows.length; f++)
 
89
                {
 
90
                        paintArrow(arrows[f], graphics);
 
91
                }
 
92
        }
 
93
 
 
94
        void paintArrow(Arrow arrow, Graphics2D graphics)
 
95
        {
 
96
                Dimension ss = r2dm.getBackgroundDimension();
 
97
                Arc2D.Double arc = contructArc(arrow);
 
98
                graphics.draw(arc);
 
99
                Polygon polygon = contructArrowHead(arc);
 
100
                graphics.fill(polygon);
 
101
                graphics.draw(contructArrowHead(arc));
 
102
                graphics.draw(new Ellipse2D.Double(center.x, ss.getHeight() - center.y, 10,10));
 
103
        }
 
104
        
 
105
        public Arc2D.Double contructArc(Arrow arrow)
 
106
        {
 
107
                Dimension ss = r2dm.getBackgroundDimension();
 
108
                IAtom start = arrow.getStart();
 
109
                IAtom end = arrow.getEnd();
 
110
                Arc2D.Double arc = new Arc2D.Double();
 
111
                IAtomContainer atomContainer = start.getBuilder().newAtomContainer();
 
112
                atomContainer.addAtom(start);
 
113
                atomContainer.addAtom(end);
 
114
                center = GeometryTools.get2DCenter(atomContainer,r2dm.getRenderingCoordinates());
 
115
                Point2d point1 = new Point2d((Point2d)r2dm.getRenderingCoordinate(start));
 
116
                Point2d point2 = new Point2d((Point2d)r2dm.getRenderingCoordinate(end));
 
117
                Vector2d vector1 = new Vector2d(point1);
 
118
                Vector2d vector2 = new Vector2d(point2);
 
119
                vector2.sub(vector1);
 
120
                
 
121
 
 
122
                rotAngle = GeometryTools.getAngle(vector2.x, vector2.y);
 
123
                offsetAngle = rotAngle + (Math.PI/2);
 
124
                vector3 = new Vector2d(Math.cos(offsetAngle), Math.sin(offsetAngle));
 
125
                vector3.normalize();
 
126
                vector3.scale(20);
 
127
                center.add(vector3);
 
128
                logger.debug("rotAngle: ", rotAngle * 360 / (Math.PI * 2));
 
129
                logger.debug("offsetAngle: ", offsetAngle * 360 / (Math.PI * 2));
 
130
                arc.setArcByCenter(center.x, ss.height - center.y, point1.distance(point2)/2,(rotAngle* 360 / (Math.PI * 2)),180,Arc2D.OPEN );
 
131
                return arc;
 
132
        
 
133
        }
 
134
 
 
135
        public Polygon contructArrowHead(Arc2D.Double arc)
 
136
        {
 
137
 
 
138
                Polygon polygon = new Polygon();
 
139
                double wingOffset = (Math.PI/18); 
 
140
                Vector2d vector2 = new Vector2d(Math.cos(offsetAngle - wingOffset ), Math.sin(offsetAngle - wingOffset));
 
141
                Vector2d vector3 = new Vector2d(Math.cos(offsetAngle + wingOffset), Math.sin(offsetAngle + wingOffset ));
 
142
                Point2d point2 = new Point2d(arc.getStartPoint().getX(), arc.getStartPoint().getY());
 
143
                Point2d point3 = new Point2d(arc.getStartPoint().getX(), arc.getStartPoint().getY());
 
144
                vector2.normalize();
 
145
                vector2.scale(10);
 
146
                vector3.normalize();
 
147
                vector3.scale(10);
 
148
                //Dimension ss = r2dm.getBackgroundDimension();
 
149
                point2.add(vector2);
 
150
                point3.add(vector3);
 
151
                polygon.addPoint((int)arc.getStartPoint().getX(), (int)arc.getStartPoint().getY());
 
152
                polygon.addPoint((int)point2.x, (int)point2.y);
 
153
                polygon.addPoint((int)point3.x, (int)point3.y);
 
154
                return polygon;
 
155
        }
 
156
 
 
157
        
 
158
}