# Thread: set x and y scale to be triangle?

1. Member
Join Date
Mar 2017
Posts
2
Rep Power
0

## set x and y scale to be triangle?

I was learning about the StdDraw method in class the other day and we looked at bouncing balls code to where balls bouncing inside a box.
Java Code:
```public class BouncingBalls {

public static void main(String[] args) {

// number of bouncing balls
int n = Integer.parseInt(args[0]);

// set boundary to box with coordinates between -1 and +1
StdDraw.setXscale(-1.0, +1.0);
StdDraw.setYscale(-1.0, +1.0);

// create an array of n random balls
Ball[] balls = new Ball[n];
for (int i = 0; i < n; i++)
balls[i] = new Ball();

// do the animation loop
StdDraw.enableDoubleBuffering();
while (true) {

// move the n balls
for (int i = 0; i < n; i++) {
balls[i].move();
}

// draw the n balls
StdDraw.clear(StdDraw.GRAY);
StdDraw.setPenColor(StdDraw.BLACK);
for (int i = 0; i < n; i++) {
balls[i].draw();
}
StdDraw.show();
StdDraw.pause(20);
}
}
}```
However, I was wondering how we could change this to have the balls bounce inside a triangle, rather than a box. Any thoughts? And to take it even further, how could we modify it to have the boundary for the bouncing balls not only to be the box but have it bounce off some triangle within the box?

2. ## Re: set x and y scale to be triangle?

What package is the StdDraw class in? I don't recognize it as part of the Java SE classes.
You will have to read the class's API doc to see what methods it has that might be useful for your project.

3. Member
Join Date
Mar 2017
Posts
2
Rep Power
0

## Re: set x and y scale to be triangle?

I'm not 100% sure, I just saw the code for it on princeton's book website http://introcs.cs.princeton.edu/java...alls.java.html

http://introcs.cs.princeton.edu/java.../index.php#1.5
Last edited by dahc; 03-03-2017 at 03:22 AM.

4. ## Re: set x and y scale to be triangle?

Did you read the API doc for the StdDraw class's methods? Are there any that look like they might be useful?

Is the a forum site for the StdDraw class where you could ask questions about how to use it?

5. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: set x and y scale to be triangle?

I don't understand why different organizations (ACM, Princeton, et al) feel compelled to write their own graphics libraries. Unless it is something really special (e.g. gaming libraries) I would think that native Java would suffice and serve the student better.

Regards,
Jim

6. ## Re: set x and y scale to be triangle?

I guess kids like to make bouncing ball games and classes like StdDraw allow them to quickly write a 30 line program to do that.