views:

232

answers:

3

Hello all,

I'm using Java AWT to draw lines on a panel (Line2D & and Graphics2D.drawLine) and I'm wondering how I can draw a line with tick marks, similar to:

|----|----|----|----|----|

I know the positions I'd like to draw the ticks at in advance.

The lines could be in any position, so the ticks must be drawn at an angle releative to the line itself.

My basic geometry & ability to apply it in java is failing me. :)

+8  A: 
aioobe
Interesting, I believe 'AffineTransform' is the keyword I was looking for. Google produced this tutorial with very similar components:http://www.glyphic.com/transform/applet/1intro.htmlNow I just have to understand how to apply it to my problem.
Matt
You apply it by first translating (moving) to desired start-point, then by rotating by the desired angle (atan2(dy, dx)). Combine the two transformations with AffineTransform.concatenate.
aioobe
That looks great. I need to make myself more familiar with the Transform stuff.
jjnguy
Thank you, this is perfect
Matt
+1  A: 

Things that need noting:

  • A perpendicular line has a slope of -1/oldslope.
  • In order to support lines in any direction, you need to do it parametrically
  • Thus, you have dy and dx across the original line, which means that newdx=dy; newdy=-1*dx.
  • If you have it such that <dx, dy> is a unit vector (sqrt(dx*dx+dy+dy)==1, or dx==cos(theta); dy=sin(theta) for some theta), you then just need to know how far apart you want the tick marks.
  • sx, sy are your start x and y
  • length is the length of the line
  • seglength is the length of the dashes
  • dx, dy is the slopes of the original line
  • newdx, newdy are the (calculated above) slopes of the cross lines

Thus,

  1. Draw a line from <sx,sy> (start x,y) to <sx+dx*length,sy+dy*length>
  2. Draw a set of lines (for(i=0;i<=length;i+=interval) from <sx+dx*i-newdx*seglength/2,sy+dy*i-newdy*seglength/2> to <sx+dx*i+newdx*seglength/2,sy+dy*i+newdy*seglength/2>
zebediah49
Matt
+1  A: 

I hope you know matrix multiplication. In order to rotate a line you need to multiple it by rotation matrix. (I coudln't draw a proper matrix but assume both line are not separated)

|x'| = |cos(an) -sin(an)| |x|

|y`| = |sin(an)  cos(an)| |y|

The old points are x,y and the new is x',y'. Let us illustrate by an example, lets say you have a vertical line from (0,0) to (0,1), now you want to rotate it by 90 degrees. (0,0) will remain zero so lets just see what happens to (0,1)

|x'| = |cos(90) -sin(90)| |0|

|y`| = |sin(90)  cos(90)| |1|

==

|1 0| |0|

|0 1| |1|

==

| 1*0 + 0*1|

| 0*0 + 1*1|

== |0|

   |1|

you get to horizontal line (0,0),(0,1) like you would expect.

Hope it helps,
Roni

roni