Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to set random X,Y coordinates within a TRIANGLE?

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to set random X,Y coordinates within a TRIANGLE?

    I am practicing inheritance using a graphics program with the Expo class. So far I've got the inheritance part, my problem is with one of the methods of my XmasTree class. Altogether the program is supposed to draw a pine tree (a green triangle, really) while the XmasTree subclass is supposed to draw ornaments (red circles) on the tree.

    To do that, I need to be able to set random coordinates within the area of the triangle - but I have no idea how. I know how to limit the range of the Expo.random method so that it finds coordinates inside a shape with consistent width and height, like a rectangle or such. My question is, since the triangle that the ornaments need to be within tappers toward the top, how can I write a code which makes the random method find a number that is within a smaller range of X coordinates, the higher up the triangle the Y coordinate is?

    The code I wrote currently creates a red circle that is always to the left of, or on the left edge of, the triangle, but not within the triangle like it needs to be. I got there by figuring that by finding the slope of the triangle, if I multiply the number of units the Y coordinate is above the base of the triangle by the slope and then subtract that amount from the range of the X coordinates at the base, perhaps that would account for the tapering of the triangle, but it hasn't and I'm not sure that even makes sense... My variable naming may have gotten a bit confusing at the end, please tell me if you need further elaboration.


    // Lab09GRFX05st.java
    // This is the student, starting file of the Lab09GRFX05 assignment.
    // This file provides the completed Tree class which is the superclass for the PineTree class.
    // PineTree is partially written.  Students need to complete it.
    // XmasTree will inherit from PineTree and is not provided.
     
     
    import java.awt.*;
    import java.applet.*;
     
     
    public class Lab09GRFX05st extends Applet
    {
    	public void paint(Graphics g)
    	{
    		XmasTree tree = new XmasTree();
    		tree.drawTrunk(g);
    		tree.drawLeaves(g);
    		tree.drawOrnament(g);
     
    	}
    }
     
     
    class Tree
    {
    	protected int baseCenterX;
    	protected int baseCenterY;
    	protected int trunkWidth;
    	protected int trunkHeight;
     
    	public Tree()
    	{
    		baseCenterX = 475;
    		baseCenterY = 600; 
    		trunkWidth = 50;
    		trunkHeight = 200;
    	}
     
    	public void drawTrunk(Graphics g)
    	{
    		Expo.setColor(g,128,64,0);
    		int tlX = baseCenterX - (trunkWidth/2);
    		int tlY = baseCenterY - trunkHeight;
    		int brX = baseCenterX + (trunkWidth/2);
    		int brY = baseCenterY;
    		Expo.fillRectangle(g,tlX,tlY,brX,brY);;
    	}	
     
    	public void drawLeaves(Graphics g)
    	{
    		Expo.setColor(g,Expo.green);
    		int radius = 100;
    		int centerX = baseCenterX;
    		int centerY = baseCenterY - (trunkHeight + radius - 5);
    		Expo.fillCircle(g,centerX,centerY,radius);
    	}			
    }
     
     
    class PineTree extends Tree
    {
     
    	protected int leavesWidth;
    	protected int leavesHeight;
     
    	public PineTree()
    	{
    		leavesWidth = 300;
    		leavesHeight = 350;	
    	}
     
    	public void drawLeaves(Graphics g)
    	{
    		Expo.setColor(g,Expo.green);
    		//center point
    		int centerX = baseCenterX;
    		int centerY = baseCenterY - (trunkHeight + leavesHeight);
    		//left point
    		int leftX = baseCenterX - (leavesWidth/2); 
    		int leftY = baseCenterY - trunkHeight;
    		//right point
    		int rightX = baseCenterX + (leavesWidth/2);
    		int rightY = baseCenterY - trunkHeight;
     
    		//(Temporary) Displays point coordinates so I can see what they are
    		g.drawString(" "+centerX, centerX, centerY);
    		g.drawString(" "+leftX, leftX-25, leftY);
    		g.drawString(" "+rightX, rightX, rightY);
    		//
    		Expo.fillTriangle(g,centerX,centerY,leftX,leftY,rightX,rightY);
    	}			
     
    }
     
    class XmasTree extends PineTree
    {
     
    public XmasTree()
    {
    	leavesWidth = 300;
    	leavesHeight = 350;	
    }
     
    	public void drawOrnament(Graphics g)
    	{
    		Expo.setColor(g,Expo.red);
    		int radius = 10;
    		int centerX = baseCenterX;
    		int centerY = baseCenterY - (trunkHeight);
     
    		//sets random location for bulbs
    		int maxH = 50 + radius;
     		int minH = 400 - radius;
     		//Slope of triangle:
     		int slope= 7/3;
     		//random height:
     		int bulbH = Expo.random(maxH,minH);
     
     		for(int k=1; k < 10; k++){
     		}	
     		if(bulbH < minH){
     			int rightW = 625;
     			int leftW = 325;
     			int bHeight = minH - bulbH;
     			int rangeW = bHeight*slope;
     		 	rightW = rightW - rangeW;
     			leftW = leftW + rangeW;
     			int bulbW = Expo.random(leftW,rightW);
     			Expo.fillCircle(g,bulbH,bulbW,radius);
     		}
     		}
     
     
    	}


    The HTML file is:
    HTML Code:
    <APPLET CODE = "Lab09GRFX05st.class" WIDTH=1000 HEIGHT=650>
    </APPLET>


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to set random X,Y coordinates within a TRIANGLE?

    One way to get points within a shape would be to use a class that implements the Shape interface. For example: Polygon. Shape has a contains method. You could randomly generate a pair of numbers: x,y and call the contains method to test if they are inside of the shape.

    how can I write a code which makes the random method find a number that is within a smaller range of X coordinates, the higher up the triangle the Y coordinate is?
    Take a piece of grid paper and draw the shape. Then look at the width of the shape as the y value changes towards the top. To be within the shape you need to add on an x value that is determined by the y value and then generate a random number in the range of the width of the shape at that y value.

    You need just a bit of math to get these values.
    Last edited by Norm; February 5th, 2012 at 08:15 AM.

Similar Threads

  1. Help with plotting x-y coordinates.
    By mixmagz in forum Object Oriented Programming
    Replies: 5
    Last Post: January 23rd, 2012, 09:41 AM
  2. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  3. Mapping real coordinates into GUI
    By gloor in forum AWT / Java Swing
    Replies: 2
    Last Post: May 26th, 2011, 08:35 AM
  4. Retrieve coordinates of other windows
    By Knox in forum AWT / Java Swing
    Replies: 1
    Last Post: April 10th, 2011, 02:03 PM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM