001/*
002
003*/
004
005package renderer.scene;
006
007import java.awt.Color;
008
009/**
010   A {@code LineSegment} object has two integers that
011   represent the endpoints of the line segment. The
012   integers are indices into the {@link Vertex} list
013   of a {@link Model} object.
014*/
015/*
016   NOTE: The LineSegment class could be an inner class of
017         the Model class. Then each LineSegment object would
018         automatically have acces to the actual Vertex list
019         that the LineSegment is indexing into. Here, we
020         have every LineSegment object hold a reference to
021         the Model object that it is "part of".
022*/
023public class LineSegment
024{
025   public int[] index = new int[2]; // the indices for this line segment
026   public Model model;
027
028   /**
029      Create a {@code LineSegment} object with two integer indices
030      into a {@link Model}'s list of {@link Vertex} objects.
031
032      @param i0     index of 1st endpoint of the new {@code LineSegment}
033      @param i1     index of 2nd endpoint of the new {@code LineSegment}
034      @param model  {@link Model} containing the {@link Vertex} list for the new {@code LineSegment}
035   */
036   public LineSegment(int i0, int i1, Model model)
037   {
038      this.model = model;
039      index[0] = i0;
040      index[1] = i1;
041   }
042
043
044   /**
045      Create a {@code LineSegment} object with the same two indices
046      from the given {@code LineSegment} object but referring to
047      the {@link Vertex} list from another {@link Model}.
048
049      @param ls     {@code LineSegment} to make a copy of
050      @param model  {@link Model} that the new {@code LineSegment} is part of
051   */
052   public LineSegment(LineSegment ls, Model model) // a "copy constructor"
053   {
054      this( ls.index[0], ls.index[1], model );
055   }
056
057
058   /**
059      Give this {@code LineSegment} a uniform {@link Color}.
060
061      @param c  {@link Color} for this LineSegment
062   */
063   public void setColor(Color c)
064   {
065      model.vertexList.get(index[0]).setColor(c);
066      model.vertexList.get(index[1]).setColor(c);
067   }
068
069   /**
070      Give this {@code LineSegment} a uniform,
071      but randomly chosen, {@link Color}.
072   */
073   public void setColorRandom()
074   {
075      Vertex v0 = model.vertexList.get(index[0]);
076      Vertex v1 = model.vertexList.get(index[1]);
077      v0.setColorRandom();
078      v1.setColor(v0);
079   }
080
081   /**
082      Give each {@link Vertex} of this {@code LineSegment}
083      a randomly chosen {@link Color}.
084      <p>
085      NOTE: This works best when this {@link LineSegment}
086      object does not share its {@link Vertex} objects
087      with another {@code LineSegment} object..
088   */
089   public void setColorRainbow()
090   {
091      model.vertexList.get(index[0]).setColorRandom();
092      model.vertexList.get(index[1]).setColorRandom();
093   }
094
095
096   /**
097      For debugging.
098
099      @return {@link String} representation of this {@code LineSegment} object
100   */
101   @Override
102   public String toString()
103   {
104      return "Line Segment: (" + index[0] + ", " + index[1] + ")\n";
105   }
106}