Okay so I\'m supposed to plot a couple thousand of points using the following ru
ID: 3631090 • Letter: O
Question
Okay so I'm supposed to plot a couple thousand of points using the following rules
double[] cx = { 0.000, 1.000, 0.500 };
double[] cy = { 0.000, 0.000, 0.866 };
double x = 0.0, y = 0.0;
for (int i = 0; i < N; i++) {
int r = StdRandom.uniform(3);
x = (mx + cx[r]) / 2.0;
y = (my + cy[r]) / 2.0;
StdDraw.point(x, y); }}
to get Sierpinski's triangle. I can do that easily using a file for data input
java Siernpinski 10000 < sierpinski.txt where the text contains the following
but the catch is, I'm supposed to hard code this into the program AND have the starting point be user input via mouse click. I don't know how to put these two together and I keep running into problems. Here is what I have so far. Please help.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
public class Sierpinski extends Applet implements MouseListener, MouseMotionListener{
int width, height;
int mx,my;
boolean isButtonPressed = false;
public void init(){
width=getSize().width;
height=getSize().height;
setBackground(Color.white);
mx=width/2;
my=height/2;
addMouseListener(this);
addMouseMotionListener(this);}
public void paint (Graphics g){
int[] xPoints={195,65,325};
int[] yPoints={30,255,255};
g.drawPolygon(xPoints,yPoints,3);}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){
mx=e.getX();
my=e.getY();
System.out.println(mx+","+my);
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseDragged(MouseEvent e){}
public static void main (String args[]){
Scanner Keyboard = new Scanner(System.in);
int N;
System.out.println("Welcome to the Interactive Sierpinski Triangle Java App");
System.out.println("Please enter the number of iterations that you would like to see");
N = Keyboard.nextInt();
double[] cx = { 0.000, 1.000, 0.500 };
double[] cy = { 0.000, 0.000, 0.866 };
double x = 0.0, y = 0.0;
for (int i = 0; i < N; i++) {
int r = StdRandom.uniform(3);
x = (x + cx[r]) / 2.0;
y = (y + cy[r]) / 2.0;
StdDraw.point(x, y); }}
}
Explanation / Answer
import javaawt*;import javaxswing*;
public class Sierpinski extends JFrame
{
/**make a new Sierpinski*/
public Sierpinski()
{
//add STPane to frame
thisadd(new STPane());
//pack
thispack();
//set visible
thissetVisible(true);
}
/**main*/
public static void main(String[] args){
//make a new frame
new Sierpinski();
}
}
/**
* Point class holds a dimentional integer */
class Point {
//two portions of the point
int x,y;
//default constructor does nothing
public Point(){}
//constructor that takes two arguments
public Point(int nx,int ny){
x=nx;
y=ny;
}
}
/**
* class that computes and displays the Sierpinski Triangle
*/
class STPane extends JComponent {
//holds the initial triangle for the sierpenski
Point[] triangle = new Point[];
//first point
Point fp;
//current point
Point cp=new Point();
//default constructor simply sets the original size
public STPane(){
thissetPreferredSize(new Dimension(,));
}
//simple method to ge a - range random number to select a new point
public int randto(){
return (int)(Mathrandom()*)%;
}
//paint where the magic of the chaos happens
public void paint(Graphics g){
//get the current size of the component
Dimension d = supergetSize();
//make an isocilese triangle from the size of the
//component for the original points
triangle[] = new Point((int)dgetWidth()/,);
triangle[] = new Point(,(int)dgetHeight());
triangle[] = new Point((int)dgetWidth(),(int)dgetHeight());
//get an arbitrary point in the middle of the screen somewhere
fp=new Point((int)(Mathrandom()*dgetWidth()),(int)(Mathrandom()*dgetHeight()/));
//get a random triangle point
Point p = triangle[randto()];
//get the first current point by going half way from the first
//point to the chosen triangle point
cpx=(px+fpx)/;
cpy=(py+fpy)/;
//draw the first point
gdrawRect(cpx,cpy,,);
//compute points in the same fashion but use the current
//point rather than the very original point
for(int i = ; i<;i++){
p = triangle[randto()];
cpx=(px+cpx)/;
cpy=(py+cpy)/;
gdrawRect(cpx,cpy,,);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.