Fractal Triangle - Recursion To Make Children Triangles

Oct 7, 2014

I have a FractalGUI and a FractalTriangle class. The FractalTriangle class is what does a lot of the work, it uses ATriangle(s) to make a triangle, and it uses recursion to draw three children triangles along each parent triangle. Fractal Triangle has a method "makeTriangle" private ATriangle makeTriangle( Point p0, Point p1 ) which takes the ATriangles and sets them the right size and everything, but for some reason I can only get one level of children triangles, and they are also already their upon running which they shouldn't be, they should appear when the depth is 2. When the depth is three it should add three triangles each onto the three added at depth two. At depth 4 it should add three each onto the three previously made triangles, and so on until depth reaches 6, the max depth. Here is some code of my FractalTriangle.

The Atriangle class given to us ("starter code") does not extend or implement anything, and has basic methods (setLocation, setSize, setThickness, setColor, etc. )

//---------------- class variables ------------------------------
//---- recursive generation parameters
public static double sizeRatio = 0.5; // integer represent %
public static double offset = 0.5; // offset/100 = parametric value
// child positioning offset
public static double p2projection = 0.5;
// parametric value of projection of
// vertex p2 onto the base; can be < 0
public static boolean outside = true;

[code]....

the way it looks without the recursion looks much more right, but I am supposed to have recursion in that constructor but I can't figure out how to get it right with the recursion. It's so weird how with the recursion the big red triangle ( base triangle ) disappears, and you can see the children are really really small.

View Replies


ADVERTISEMENT

User Input 3 Sides - Output Invalid If Does Not Make A Triangle

Nov 4, 2014

I had to make a program that allows the user to enter 3 sides and it should output "invalid" if it does not make a triangle and if it does make a triangle it calculates the area of the triangle and it doesnt seem to do anything but say "invalid" as the out put.

import java.util.Scanner;
public class MyTriangle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean valid = true;
double side1, side2, side3, area, A;
 
[Code] ....

View Replies View Related

Returns Area Of Triangle / Unless Triangle Not Value So Return String?

Dec 28, 2014

I'm doing a problem where the area of a triangle is returned (if valid). However, I want to return a message (i.e. 'triangle is not valid) if the triangle is invalid.

I'm not sure how to go about to doing this as my method (called area) will only let me return doubles. Possible to return a string in an else within my area method?

public class MyTriangle {
public static void main(String[] args) {
//triangle is valid if the sum of any two sides is bigger than the third
System.out.println(isValid(3, 4, 5));
System.out.println(area(543, 4, 5));

[Code] ...

View Replies View Related

Swing/AWT/SWT :: Draw 3 Triangles On Top Of Each Other To Give Appearance Of A Tree

Nov 22, 2014

I'm having trouble in 2 areas.

I'm trying to draw 3 triangles 'on top' of each other to give appearance of a tree. My code is below and struggling with how to set my x/y cords correctly.

Also I've drawn a rectangle under a new class and that should then be appearing in the south location of my border layout (south) - but it's not.

import javax.swing.*;
import java.awt.*;
public class Christmas1 extends JFrame {
JPanel titlePanel;
JLabel title;

[Code] .....

View Replies View Related

Find Area Of Four Triangles Split By Two Intersecting Lines

May 4, 2014

Im doing a problem where i have to find the area of four triangles split by two intersecting lines, All i have are the points for x1, y1, x2, y2, x3, y3, x4, y4. My question is how do i find where these two lines intersect?

View Replies View Related

Read Positive Integer N And Plot 3 Triangles Of Size N

Oct 10, 2014

Write a Java program that reads positive integer n, 1 ≤ n ≤ 13, and plots 3 triangles of size n as shown below.

For n = 4, for instance, the program should plot: (works for n = 5 and n = 6 etc.)

triangle 1

1
2 3
4 5 6
7 8 9 10

triangle 2

_ _ _ 1
_ _ 3 2
_ 6 5 4
10 9 8 7

triangle 3

_ _ _ 1
_ _ 3 3 3
_ 5 5 5 5 5
7 7 7 7 7 7 7

import java.util.Scanner;
class Loops { 
void plotTriangle1(int n) {
int t = 1;
for (int i = 1; i <= n; i++) {
System.out.println(" ");
for (int j = 1; j <= i; j++) {
System.out.printf("%3d", t++);

[Code] ....

Currently compiles to this: Enter n(1-13):4
1
2 3
4 5 6
7 8 9 10

Format of the triangles, I couldn't get the spacing to work so I used underscores to replicate the blank space....

View Replies View Related

Recursion On Initialization

Apr 29, 2014

I am trying to make a game, for some reason i have begun to get a java.lang.StackOverflowError.

I am not exactly sure how i can fix it. only removing line 14 from infopannel1 (and everything that used that class.) seems to work. im not sure what else i can do to fix it. or why its resulting in stack overflow for that matter.

I am putting in a link for the files i wrote this using bluej (several classes have no relevance, errorv2, demonstration, folderreadertest, ReadWithScanner, saveloadtest, menutest,rannum, and menutestTester. are all irrelivent to my problem.)

[URL] .....

View Replies View Related

How Does Recursion Work

Dec 13, 2014

how does recursion works, I don't understand why it prints al numbers going up again?This is the code

void print2(int n){
if (n<0){
out.printf(" %d",-1);
return;
}
out.printf(" %d", n);
print2(n-1)
out.printf(" %d", n);
}

this should be the output if n is 6: 6 5 4 3 2 1 0 -1 0 1 2 3 4 5 6.

View Replies View Related

Sum Of Array By Recursion

Mar 25, 2015

How to add the sum of an array with a recursion, but I don't understand how to use recursion. I just understand that it calls back the method. I am nearly done with the code.

import java.util.Scanner;
class Question1{
public static void main(String[]args){
Scanner s = new Scanner(System.in);
int size, sum;
System.out.println("Please input how many numbers will be used");
size=s.nextInt();

[Code] .....

View Replies View Related

Recursion And Lists?

Aug 6, 2014

public void myFunc(MyNode n, ArrayList<MyNode> path) {
boolean hasChildren = false;
path.add(n);
int index = path.indexOf(n);
ArrayList<MyNode> statefulPath = new ArrayList<MyNode>();

[Code] ....

I have similar code that I stepped through in a debugger. After running the code I found that it built the desired tree, in this case a root node H with left child L and right child P. I want list of lists to contain all paths from root to leaf. I expected [H, L] and [H, P]. I discovered that statefulPath is not stateful; after a recursive stack frame pops, statefulPath still contains n! But that stack frame just popped! I expected to see statefulPath be [H] in the debugger and it was [H, L]! So I later have a list [H,L,P] which I don't want. How do I make the statefulPath list I want for my algorithm?

View Replies View Related

How To Build Triangle Using Loops

Jan 16, 2014

I need to build the triangle like below. How to solve this using loops.

*
* *
* * *
* * * *
* * * * *

View Replies View Related

Properties Of A Triangle - X And Y Coordinates

Oct 29, 2014

A triangle is defined by the x- and y- coordinates of its three corner points. Compute the following the following properties of a given triangle: the lengths of all sides, the angles at all corners, the perimeter and the area. The program must prompt a user for the point coordinates. I have created a class Triangle and a class TriangleSimulator, I am stuck and can't figure out why my program won't run correctly.

import java.util.Scanner;
public class Triangle {
Scanner in = new Scanner(System.in);
private int x1;
private int x2;
private int x3;

[Code] ....

View Replies View Related

Programming About Finding Triangle

Nov 25, 2014

class triangle
{
public static void main (String[] args)
{
System.out.println("Provide three side lengths - 000 to terminate.");
int a = In.getInt();
int b = In.getInt();
int c = In.getInt();
 
[code]....

My problem is that when I enter 5,2,5 it should be isosceles and acute but it comes out as isosceles and obtuse, and when I type 5,5,5 it comes out equilateral and right. The only one that works is if I enter 3,5,4 it will come out as scalene and right. I been at this for a while and my math looks correct.

View Replies View Related

Triangle Pattern (For Loop)

Feb 21, 2015

I'm trying to make a triangle which should look like this. But I cant seem o figure it out.

1
2 1
4 2 1
8 4 2 1
16 8 4 2 1
32 16 8 4 2 1
64 32 16 8 4 2 1
128 64 32 16 8 4 2 1

This is the code I have written so far.

public class TestProgram
{
public static void main(String[]args)
{
for (int columns=0; columns<=8; columns++)
{
for (int rows=columns; rows>=1; rows --)
{
System.out.print(rows+ " ");
}
System.out.println();
}
}
}

View Replies View Related

How To Get Main Triangle Program To Run

Oct 17, 2014

I am having trouble getting my main triangle program to run. My teacher gave us a sample program, so I tried running his, and it doesn't run either.

Here is the class:
 
import java.util.Scanner;
 public class ShelbyHarms_3_06_Triangle {
private double sideA, sideB, sideC; // Instance variables, numbers for area and perimeter
static Scanner console = new Scanner(System.in); // Establish keyboard
 
[Code] .....

My errors for the main program(the class has no errors) are:

helbyHarms_3_06.java:37: error: cannot find symbol
theSides.getSides();
^
symbol: variable theSides
location: class ShelbyHarms_3_06

[Code] ....

5 errors

View Replies View Related

Increment Counter By Recursion

Mar 22, 2015

I have a question related to the code below, that I do not understand. The aim is to count all files and subdirectories in an ArrayList full of files and subdirectories. So I have to count every file and every subdirectory.

The code concerning counting files is clear to me - every time d is of the type file I have to increment n by one. However I thought that I have to do the same thing in case d is a directory, so I would have written the same code for directories.

So what does "n += ((Directory) d).countAllFiles();" mean? In my understanding the method countAllFiles() is applied again on the object Directory ( as Directory is the class that contains this method), but how is n incremented by this? I thought n should be incremented by one as we did with files.

public int countAllFiles() {
int n = 0;
for(SystemFile d : content) {
if(d instanceof File) {
n++;

[Code] ....

View Replies View Related

How Recursion Calls Are Made

Jun 5, 2014

How the recursion works. I tried to figure out writing down low, mid, high at each recursive call. But I seem to be making a mistake somehow. I don't understand where the values are returned to in

if(leftmax>rightmax){
return leftmax;}
else
return rightmax;

Here's the code:

public class Maximum{
public static int max(int[] a,int low,int high){
int mid,leftmax,rightmax;
if(low==high)
return a[low];

[Code] ....

firstly leftmax=max(a,0,4)

Then what is the next line executed?Is it rightmax=max(a,5,8).

After this is it leftmax=max(a,5,6)

rightmax=max(a,7,8)

I tried to understand what these recursion calls by writing them down.But I somehow make a mistake.

View Replies View Related

Creating Palindromes Using Recursion?

Oct 20, 2014

I just started studying recursion and I wanted to know how to create a palindrome number going up from 1 to n then back to 1 like this: "12345...n...54321".

I've done one going downwards and then upwards like this: "n...4321234...n".

Here's my code:

Java Code: import java.util.*;
public class PalindromeTest
{
public static void downPalindrome(int n)

[Code].....

View Replies View Related

Printing Pattern Using Recursion

Jul 16, 2014

The program I'm working on is supposed to read input from a file and using recursion, print the pattern of asterisks for each value until the value is either < 0 or > 25.For example, if the value was 4, the pattern would look like this

*
* *
* * *
* * * *
* * *
* *
*

The values are stored in a file entitled prog3.dat which looks like this

4
3
15
26
0

I've never used recursion before and haven't been able to find anything showing how it would work with this particular type of problem.Here is what I've been able to come up with so far, but I'm having problems still which I will show following the code.

import java.util.Scanner;
import java.io.*;
public class Program3 {
public static void main(String[] args) throws Exception {
int num = 0;
java.io.File file = new java.io.File("../instr/prog3.dat");
Scanner fin = new Scanner(file);

[code]...

It appears to be reading the file correctly, but is only printing the top half of the pattern. Also, like I said, I'm not very familiar with recursion, so am not sure if this is actually recursion?

View Replies View Related

Recursion Pattern In Java

Jul 16, 2014

The program I'm working on is supposed to read input from a file and using recursion, print the pattern of asterisks for each value until the value is either < 0 or > 25. For example, if the value was 4, the pattern would look like this
 
*
* *
* * *
* * * *
* * *
* *
*
 
The values are stored in a file entitled prog3.dat which looks like this
 
4
3
15
26
0
 
I've never used recursion before and haven't been able to find anything showing how it would work with this particular type of problem. Here is what I've been able to come up with so far, but I'm having problems still which I will show following the code.
 
import java.util.Scanner; 
import java.io.*;
public class Program3 {
public static void main(String[] args) throws Exception {
int num = 0;
java.io.File file = new java.io.File("../instr/prog3.dat");
Scanner fin = new Scanner(file);

[Code] ....
 
Output:
 
Please enter an integer
*
* *
* * *
* * * *
Please enter an integer
*
* *
* * *
Please enter an integer
 
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
Please enter an integer
Please enter an integer
 
As you can see, I don't know how to make it print the pattern like in the example and am honestly not even sure if this is recursion since I've never actually worked with recursion before.

View Replies View Related

Print Triangle Shape Using Numbers?

Jun 1, 2014

i want to print triangle shape using number like

this

1

12

123

1234

12345

123456

this is my code

class shape{
public static void main(String [] args){
for(int x =0 ; x<=6;x++){
for(int y =0 ; x > y ; y++){
System.out.print(x);
}
System.out.print("
");
}
}
}

but my output is

1

22

333

4444

55555

666666

View Replies View Related

Area Of Triangle Java Program?

Sep 2, 2014

I've been having trouble with this code for about a week and I've finally got it down to one error. Here is the code:

import java.util.Scanner;
public class Triangle {
public static void main (String[] args) {
Scanner Console = new Scanner(System.in);
System.out.print("Please enter the three lengths of your Triangle: ");
double a = console.nextDouble();

[Code] ....

And here is the error:

Triangle.java:30: error: class, interface, or enum expected
} // End class
^
1 error

View Replies View Related

How To Create A Dragable Triangle With JavaFX

Nov 6, 2014

I'm trying to make a triangle that plots the point in where the corners show their position in the window and also that the user may drag each corner to a desired position.

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.scene.input.MouseButton;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Shape;

[code]....

View Replies View Related

Why Program Not Printing Square And Triangle

Nov 21, 2014

import java.util.Scanner;
public class justin10a

public static void main(String [] args)
{
int n;
n = getSize();
 
[Code] .....

View Replies View Related

Print Triangle - Loop Is Not Working

Jul 7, 2014

I am trying to make a program that prints triangle... and I did various test on each method to realise that the problem lies with this segment.When I call this method, nothing prints out, I figure there is something with the loop that I am not realizing.the loop is backwards because it's supposed to have the right side edge parralel (when I try to print it out the spaces do not appear, imagine the x are space...), so as each line is looped the # of spaces diminishes

xxxx*
xxx*x*
xx*xx*
x*xxx*
*****

public class test {
public static void main(String[] args){  
for (int countdown = 5; countdown <= 1; countdown = countdown--){
showNTimes(countdown, ' ');
showNTimes(5- countdown, '*');
System.out.println("");
}
}
public static void showNTimes ( int nbTimes, char carac ) {
for ( int i = 1 ; i <= nbTimes ; i = i + 1 ) {
System.out.print( carac );
}
}
}

View Replies View Related

JavaFX 2.0 :: Triangle Mesh Rendering

Aug 7, 2014

I'm new to JavaFX and I'm exploring its 3D capabilities, and in particular the TriangleMesh visualisation.
 
I have developed a very basic application, based on the Molecule Sample Application provided in the examples, that read a mesh file generated by an external tool (Gmsh) and displays it. Pretty basic.
 
My problem is that the rendering is not what I expected (see attachement)
  
My guess is that I got something wrong with the texture coordinates or the triangles orientation.

- I don't what to have textures attached to my meshes. These are 3D meshes used for scientific simulation purposes. I thus have set all texCoords to 0.
- I don't control the orientation of my triangles, they are generated by Gmsh. Their orientation is also not interesting for my end users so I would like to display all triangles the same way. I used the meshView.setCullFace(CullFace.NONE); method.

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved