001/*
002 * Renderer Models. The MIT License.
003 * Copyright (c) 2022 rlkraft@pnw.edu
004 * See LICENSE for details.
005*/
006
007package renderer.models_L;
008
009import renderer.scene.*;
010import renderer.scene.primitives.*;
011
012import java.awt.Color;
013
014/**
015   Create an x and y axis in the xy-plane, along with "tick marks".
016*/
017public class Axes2D extends Model
018{
019   /**
020      Create an x and y axis from -1 to +1 on each axis.
021      The default {@link Color} is white.
022   */
023   public Axes2D( )
024   {
025      this(-1, 1, -1, 1, 5, 5);
026   }
027
028
029   /**
030      Create an x-axis from {@code xMin} to {@code xMax}
031      and a y-axis from {@code yMin} to {@code yMax}.
032      The default {@link Color} is white.
033
034      @param xMin    left end point for the x-axis
035      @param xMax    right end point for the x-axis
036      @param yMin    bottom end point for the y-axis
037      @param yMax    top end point for the y-axis
038      @param xMarks  number of evenly spaced tick marks on the x-axis
039      @param yMarks  number of evenly spaced tick marks on the y-axis
040   */
041   public Axes2D(final double xMin, final double xMax,
042                 final double yMin, final double yMax,
043                 final int xMarks,  final int yMarks)
044   {
045      this(xMin, xMax, yMin, yMax, xMarks, yMarks, Color.white);
046   }
047
048
049   /**
050      Create an x-axis from {@code xMin} to {@code xMax}
051      and a y-axis from {@code yMin} to {@code yMax}.
052      Use the given {@link Color} for both axes.
053
054      @param xMin    left end point for the x-axis
055      @param xMax    right end point for the x-axis
056      @param yMin    bottom end point for the y-axis
057      @param yMax    top end point for the y-axis
058      @param xMarks  number of evenly spaced tick marks on the x-axis
059      @param yMarks  number of evenly spaced tick marks on the y-axis
060      @param c       {@link Color} for both axes
061   */
062   public Axes2D(final double xMin, final double xMax,
063                 final double yMin, final double yMax,
064                 final int xMarks,  final int yMarks,
065                 final Color c)
066   {
067      this(xMin, xMax, yMin, yMax, xMarks, yMarks, c, c);
068   }
069
070
071   /**
072      Create an x-axis from {@code xMin} to {@code xMax}
073      and a y-axis from {@code yMin} to {@code yMax}.
074      Use the given {@link Color} for each axis.
075
076      @param xMin    left end point for the x-axis
077      @param xMax    right end point for the x-axis
078      @param yMin    bottom end point for the y-axis
079      @param yMax    top end point for the y-axis
080      @param xMarks  number of evenly spaced tick marks on the x-axis
081      @param yMarks  number of evenly spaced tick marks on the y-axis
082      @param cX      {@link Color} for the x-axis
083      @param cY      {@link Color} for the y-axis
084   */
085   public Axes2D(final double xMin, final double xMax,
086                 final double yMin, final double yMax,
087                 final int xMarks,  final int yMarks,
088                 final Color cX,    final Color cY)
089   {
090      this(xMin, xMax, yMin, yMax, xMarks, yMarks, cX, cY, 0.0);
091   }
092
093
094   /**
095      Create an x-axis from {@code xMin} to {@code xMax}
096      and a y-axis from {@code yMin} to {@code yMax}.
097      Use the given {@link Color} for each axis.
098   <p>
099      The {@code z} parameter is so that we can put the axis just above
100      or just below the xy-plane (say {@code z=0.01} or {@code z=-0.01}).
101      This way, the axes can be just in front of or just behind whatever
102      is being drawn in the xy-plane.
103
104      @param xMin    left end point for the x-axis
105      @param xMax    right end point for the x-axis
106      @param yMin    bottom end point for the y-axis
107      @param yMax    top end point for the y-axis
108      @param xMarks  number of evenly spaced tick marks on the x-axis
109      @param yMarks  number of evenly spaced tick marks on the y-axis
110      @param cX      {@link Color} for the x-axis
111      @param cY      {@link Color} for the y-axis
112      @param z       offset of the axes away from the xy-plane
113   */
114   public Axes2D(final double xMin, final double xMax,
115                 final double yMin, final double yMax,
116                 final int xMarks,  final int yMarks,
117                 final Color cX, final Color cY,
118                 final double z)
119   {
120      super("Axes 2D");
121
122      addColor(cX, cY);
123
124      // x-axis
125      addVertex(new Vertex(xMin, 0, z),
126                new Vertex(xMax, 0, z));
127      addPrimitive(new LineSegment(0, 1, 0)); // use color cX
128
129      // y-axis
130      addVertex(new Vertex(0, yMin, z),
131                new Vertex(0, yMax, z));
132      addPrimitive(new LineSegment(2, 3, 1)); // use color cY
133
134      int index = 4;
135
136      // Put evenly spaced tick marks on the x-axis.
137      double xDelta = (xMax - xMin)/xMarks,
138             yDelta = (yMax - yMin)/50;
139      for (double x = xMin; x <= xMax; x += xDelta)
140      {
141         addVertex(new Vertex(x,  yDelta/2, z),
142                   new Vertex(x, -yDelta/2, z));
143         addPrimitive(new LineSegment(index+0, index+1, 0)); // use color cX
144         index += 2;
145      }
146
147      // Put evenly spaced tick marks on the y-axis.
148      yDelta = (yMax - yMin)/yMarks;
149      xDelta = (xMax - xMin)/50;
150      for (double y = yMin; y <= yMax; y += yDelta)
151      {
152         addVertex(new Vertex( xDelta/2, y, z),
153                   new Vertex(-xDelta/2, y, z));
154         addPrimitive(new LineSegment(index+0, index+1, 1)); // use color cY
155         index += 2;
156      }
157   }
158}//Axes2D