2 Replies - 318 Views - Last Post: 04 February 2012 - 03:02 PM Rate Topic: -----

Topic Sponsor:

#1 r3dApple  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-February 12

How would I set random X,Y coordinates within a TRIANGLE?

Posted 04 February 2012 - 12:16 PM

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:
<APPLET CODE = "Lab09GRFX05st.class" WIDTH=1000 HEIGHT=650>
</APPLET>



And in case anyone is unable to find the Expo class, I've attached it to this post.

Is This A Good Question/Topic? 0
  • +

Replies To: How would I set random X,Y coordinates within a TRIANGLE?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3239
  • View blog
  • Posts: 10,644
  • Joined: 18-April 07

Re: How would I set random X,Y coordinates within a TRIANGLE?

Posted 04 February 2012 - 02:55 PM

A URL I think you should check out...

http://www.blackpawn...ly/default.html

Enjoy! :)
Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is online

  • Ready for water skiing!
  • member icon

Reputation: 1067
  • View blog
  • Posts: 2,701
  • Joined: 10-September 10

Re: How would I set random X,Y coordinates within a TRIANGLE?

Posted 04 February 2012 - 03:02 PM

I've thought about this some and don't have a great answer, but I'll give you this to think about:

For any triangle with a horizontal base ( y = 0 or some constant ), and knowing the 3 points that define the triangle, you can find the equations of the sides of the triangle from the base to the zenith. Knowing the equations of those lines, you can then for any y, an elevation from the base of the triangle, find a range of x that lies between the two lines. From that you'll be able to determine if any (x, y) pair lies within the interior of your triangle.

As for changing the size of the ornaments as the tree/triangle gets narrower, I would think you'd just decrease the diameter of the ornaments as y increases.

Let us know how it's going.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1