Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

This isn't a game development question per se, but here goes...

I have a 50x50 pixel space in which I need to draw five lines: X and Y axes, and one line for each of a steepest, average, and flattest slope.

I'm working with (Java) code that was handed down to me with no documentation or commentary and I've never worked with drawing on a canvas before. Here's the code for a box that measured 175x75 px. This needs to be adapted for a 50x50 area as described above.

    DrawingArea canvas = new DrawingArea(175, 75);
    canvasContainer.add(canvas);
    Rectangle rectangle = new Rectangle(0, 0, 174, 75);
    canvas.add(rectangle);
    Line slopeMainLineX = new Line(5, 70, 170, 70);
    slopeMainLineX.setStrokeOpacity(0.5);
    canvas.add(slopeMainLineX);
    Line slopeMainLineY = new Line(40, 70, 40, 0);
    slopeMainLineY.setStrokeOpacity(0.5);
    canvas.add(slopeMainLineY);
    steepestLine = new Line(40, 70, 0, 0);
    steepestLine.setStrokeWidth(3);
    canvas.add(steepestLine);
    avgSlopeLine = new Line(40, 70, 0, 0);
    avgSlopeLine.setStrokeWidth(2);
    canvas.add(avgSlopeLine);
    flattestLine = new Line(40, 70, 0, 0);
    flattestLine.setStrokeWidth(1);
    canvas.add(flattestLine);

    int steepestAngle = Math.round(site.getSlope().getMax());
    int averageAngle = Math.round(site.getSlope().getAvg());
    int flatestAngle = Math.round(site.getSlope().getMin());

    double xPointSteepestAngle = 40 + 120 * Math.cos(steepestAngle*0.0174532925);
    double yPointSteepestAngle = 70 + 120 * Math.sin(steepestAngle*0.0174532925);
    double xPointAverageAngle = 40 + 120 * Math.cos(averageAngle*0.0174532925);
    double yPointAverageAngle = 70 + 120 * Math.sin(averageAngle*0.0174532925);
    double xPointFlatestAngle = 40 + 120 * Math.cos(flatestAngle*0.0174532925);
    double yPointFlatestAngle = 70 + 120 * Math.sin(flatestAngle*0.0174532925);

    steepestLine.setX2((int) xPointSteepestAngle);
    steepestLine.setY2(70 - ((int) yPointSteepestAngle - 70));

    avgSlopeLine.setX2((int) xPointAverageAngle);
    avgSlopeLine.setY2(70 - ((int) yPointAverageAngle - 70));

    flatestLine.setX2((int) xPointFlatestAngle);
    flatestLine.setY2(70 - ((int) yPointFlatestAngle - 70));

I'm completely lost here, so any help would be greatly appreciated.

share|improve this question
    
too...many...magic..numbers..... head explodes –  pek Aug 25 '11 at 15:31
    
Off-topic and too localized. Did you try stackoverflow? –  bummzack Aug 25 '11 at 21:31

1 Answer 1

up vote 5 down vote accepted

Replace some constants, it might help understanding what happens:

0.0174532925 => Math.PI / 180.0
175 => MyCanvas.width
75 => MyCanvas.height
174 => MyCanvas.width - 1
74 => MyCanvas.height - 1
5 => MyCanvas.padding
170 => MyCanvas.width - MyCanvas.padding
70 => MyCanvas.height - MyCanvas.padding
40 => (MyCanvas.height - MyCanvas.padding) / 2 + MyCanvas.padding
120 => MyCanvas.slopeLineLength /* About 40 */

Alternatively, to make the horizontal line centered, replace the following value:

40 => MyCanvas.height / 2 /* 37.5 in original, not 40.0 */

Finally, you can calculate the slope line length instead of setting it (to about 50 if you don't want the lines to be too long):

120 => Math.sqrt(
                   (MyCanvas.height / 2 - MyCanvas.padding)
                   * (MyCanvas.height / 2 - MyCanvas.padding)
                 + (MyCanvas.width - MyCanvas.padding * 2) 
                   * (MyCanvas.width - MyCanvas.padding * 2))

EDIT: Scratch the last part, obvious brain fart on my side. You should just pick a value between (MyCanvas.height - 2 * MyCanvas.padding) / 2 and (MyCanvas.width - 2 * MyCanvas.padding) for MyCanvas.slopeLineLength.

share|improve this answer
    
+1 For replacing constants, pretty much can't go wrong there. –  Byte56 Aug 25 '11 at 17:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.