Draw a Circle With Radius Using 2d Array in C
#1
cartoon a filled rectangle/circle in a 2d array
Posted sixteen October 2011 - 07:04 PM
Hey, I'one thousand having bug writing methods rectangle and circle that draws a filled rectangle and circle respectively.
This program is supposed to create a grade Movie which includes methods Picture(int rmax, int cmax, char background) which draws the board, print() which prints the receiving picture, rectangle(int rlo, int rhi, int clo, int chi, char color) which draws a filled rectangle in the second array where rlo, rhi, clo, chi define the corners of the rectangle and color is the character to put in teach pixel of the rectangle, method circle(double rc, double cc, double radius, char color) draws a filled circle in a 2d array where (rc,cc) is the middle radius is the radius and color is the character to put in each pixel of the circle.
in the circumvolve method nosotros are supposed to utilise a an algorithm that considers every pixel (r,c) in the array and if the distance from (r,c) to (rc,cc) is less than or equal to radius then requite information technology the specified color. after all that we write a commuter which tests the course.
i know you lot won't do my homework for me, im just a fleck dislocated on what needs to be done
the lawmaking i take so far
I'1000 not quite sure i did the circle method correctly
i tried compiling what i had and it gives me
Main.coffee:25: cannot detect symbol
symbol : variable rmax
location: class Pic
for (int row = 0; row < rmax; row++)
^
Primary.java:27: cannot observe symbol
symbol : variable cmax
location: class Picture
for (int col = 0; col < cmax; col++)
^
Chief.java:39: cannot find symbol
symbol : variable rmax
location: form Flick
for(int row = 0; row < rmax; row++)
^
Principal.java:41: cannot find symbol
symbol : variable cmax
location: grade Picture
for(int col = 0; col < cmax; col++)
^
Main.java:53: cannot discover symbol
symbol : constructor Flick(char[][])
location: grade Picture
return new Picture show(result);
^
Master.java:58: cannot find symbol
symbol : variable row
location: course Moving picture
double d = Math.sqrt((rc-row)(rc-row)+(cc-col)(cc-col));
^
Main.coffee:58: unexpected type
required: form
found : value
double d = Math.sqrt((rc-row)(rc-row)+(cc-col)(cc-col));
^
Chief.java:58: cannot find symbol
symbol : variable row
location: class Picture
double d = Math.sqrt((rc-row)(rc-row)+(cc-col)(cc-col));
^
Main.java:58: cannot find symbol
symbol : variable col
location: class Picture
double d = Math.sqrt((rc-row)(rc-row)+(cc-col)(cc-col));
^
Main.java:58: unexpected type
required: class
found : value
double d = Math.sqrt((rc-row)(rc-row)+(cc-col)(cc-col));
^
Primary.java:58: cannot find symbol
symbol : variable col
location: form Picture show
double d = Math.sqrt((rc-row)(rc-row)+(cc-col)(cc-col));
^
Principal.java:58: sqrt(double) in coffee.lang.Math cannot be applied to (coffee.lang.String)
double d = Math.sqrt((rc-row)(rc-row)+(cc-col)(cc-col));
^
Main.java:61: cannot notice symbol
symbol : variable rmax
location: class Moving-picture show
for(int row = 0; row < rmax; row++)
^
Main.java:63: cannot find symbol
symbol : variable cmax
location: class Picture
for(int col =0; col < cmax; col++)
^
Main.java:71: cannot find symbol
symbol : constructor Picture(char[][])
location: grade Picture
return new Motion picture(circumvolve);
^
// Written by // // // import java.io.*; import java.util.Scanner; //////////////////////////////////////////////////////////////////////////// course Picture { individual char[][]a; //-------------------------------------------------------------------------- public Picture(int rmax, int cmax, char background) { for(int row = 0; row < rmax; row ++) { for(int col = 0; col < cmax; col++) { a[row][col] = groundwork; } } } //-------------------------------------------------------------------------- public Motion-picture show print() // seperate method to impress the other classes { for (int row = 0; row < rmax; row++) { for (int col = 0; col < cmax; col++) { System.out.print(a[row][col] + " "); } Arrangement.out.println(); } } //-------------------------------------------------------------------------- public Picture Rectangle(int rlo, int rhi, int clo, int chi, char color) { char[][] event = new char[a.length][a.length]; for(int row = 0; row < rmax; row++) { for(int col = 0; col < cmax; col++) { if(row >= rlo && row <= rhi) { upshot[row][col] = color; } if(col >= clo && row <= chi) { result[row][col] = color; } } } render new Moving-picture show(issue); } //-------------------------------------------------------------------------- public Picture Circle (double rc, double cc, double radius, char color) { double d = Math.sqrt((rc-row)(rc-row)+(cc-col)(cc-col)); char[][] circle = new char[a.length][a.length]; for(int row = 0; row < rmax; row++) { for(int col =0; col < cmax; col++) { if(d <= radius) { circle[row][col] = color; } } } return new Picture(circle); } //-------------------------------------------------------------------------- } // end course Picture //////////////////////////////////////////////////////////////////////////// // driver that tests grade, ignore for now lol class Hw08 { //-------------------------------------------------------------------------- public static void main ( String [] args ) throws Exception { Motion-picture show p = new Picture (50,50," "); p.print(); p.rectangle(10,20,ten,20,'x'); p.impress(); p.circle(30,30,10,"o"); } //-------------------------------------------------------------------------- } // terminate form Hw08 //////////////////////////////////////////////////////////////////////////// This post has been edited past BlahBlahMan: xvi October 2011 - 07:10 PM
#2
Re: drawing a filled rectangle/circle in a second array
Posted 16 October 2011 - 07:12 PM
Recall that when yous have methods with parameters, those parameters are simply accessible inside that method. Then here public Picture(int rmax, int cmax, char groundwork), rmax and cmax are parameters. You cannot use them outside of this constructor. However, in a two-dimensional array a.length returns the number of rows, and a[someIndex].length returns the number of elements (columns) for the array at someIndex.
Quote
Main.java:53: cannot find symbol
symbol : constructor Picture(char[][])
location: course Film
return new Picture show(upshot);
^
You didn't define a constructor to accept a char[][].
Hither, you have to explicitly multiply the expressions in the parentheses together using the * operator.
double d = Math.sqrt((rc-row)(rc-row)+(cc-col)(cc-col));
Those should account for a lot of your errors.
#3
Re: drawing a filled rectangle/circumvolve in a 2d assortment
Posted 16 October 2011 - 07:24 PM
Your code is all wrong, you dont have a Picture constructor that assigns all the arrays items to whatever yous pass to it.
You declare methods of type Picture simply don't return picture.
In your driver you pass a string, when you declared a char. So you need to change that.
#iv
Re: drawing a filled rectangle/circle in a 2nd array
Posted sixteen October 2011 - 07:44 PM
im a bit confused on how to do the constructor for 2d arrays
wouuld i do something like this?
public Moving picture(char rmax, char cmax, char background) { a = new char [rmax][cmax] for(int row = 0; row < rmax; row ++) { for(int col = 0; col < cmax; col++) { a[row][col] = background; } } } revised code beneath without ^
// Written by // // description of this plan // import coffee.io.*; import java.util.Scanner; //////////////////////////////////////////////////////////////////////////// class Picture { private char[][]a; private int i; //-------------------------------------------------------------------------- public Picture(int rmax, int cmax, char background) { a = new char [rmax][cmax] for(int row = 0; row < rmax; row ++) { for(int col = 0; col < cmax; col++) { a[row][col] = background; } } } //-------------------------------------------------------------------------- public Picture impress() // seperate method to impress the other classes { for (int row = 0; row < a.length; row++) { for (int col = 0; col < a[i].length; col++) { Organization.out.print(a[row][col] + " "); } System.out.println(); } } //-------------------------------------------------------------------------- public Picture Rectangle(int rlo, int rhi, int clo, int chi, char color) { char[][] result = new char[a.length][a.length]; for(int row = 0; row < a.length; row++) { for(int col = 0; col < a[i].length; col++) { if(row >= rlo && row <= rhi) { issue[row][col] = color; } if(col >= clo && row <= chi) { result[row][col] = colour; } } } return Motion-picture show(result); } //-------------------------------------------------------------------------- public Pic Circle (double rc, double cc, double radius, char color) { char[][] circumvolve = new char[a.length][a.length]; for(int row = 0; row < a.length; row++) { for(int col = 0; col < a[i].length; col++) { double d = Math.sqrt((rc-row)*(rc-row)+(cc-col)*(cc-col)); if(d <= radius) { circle[row][col] = color; } } } return Motion-picture show(circle); } //-------------------------------------------------------------------------- } // end class Picture //////////////////////////////////////////////////////////////////////////// // driver that tests grade, ignore for now lol form Hw08 { //-------------------------------------------------------------------------- public static void main ( char [][] args ) throws Exception { Movie p = new Picture (50,50," "); p.print(); p.Rectangle(10,20,10,20,'10'); p.impress(); p.Circle(xxx,thirty,10,"o"); } //-------------------------------------------------------------------------- } // cease class Hw08 //////////////////////////////////////////////////////////////////////////// This postal service has been edited by BlahBlahMan: 16 October 2011 - 07:55 PM
#five
Re: cartoon a filled rectangle/circumvolve in a 2d array
Posted sixteen October 2011 - 07:57 PM
Hither, detect how the param is a char[][]. And then I assign the param to the instance field.
public Picture(char[][] a){ this.a = a; } #6
Re: drawing a filled rectangle/circle in a 2d assortment
Posted 16 October 2011 - 08:02 PM
macosxnerd101, on sixteen October 2011 - 07:57 PM, said:
Hither, notice how the param is a char[][]. So I assign the param to the instance field.
public Picture show(char[][] a){ this.a = a; } and then a constructor that would work would be something like this?
public Picture show(char[][] a) { this.rmax = rmax; this.cmax = cmax; this.background = groundwork; this.a = new char [rmax][cmax]; for(int row = 0; row < rmax; row ++) { for(int col = 0; col < cmax; col++) { a[row][col] = background; } } } This post has been edited by BlahBlahMan: xvi October 2011 - 08:10 PM
#7
Re: drawing a filled rectangle/circle in a 2nd array
Posted 16 October 2011 - 09:53 PM
revised lawmaking, withal confused
getting these errors
Main.coffee:123: cannot find symbol
symbol : method Picture(int,int,java.lang.String)
location: course Hw08
Picture p = Motion picture(50,50," ");
^
Master.java:125: rectangle(int,int,int,int,char) in Picture cannot be applied to (int,int,int,int,java.lang.String)
p.rectangle(10,twenty,x,20,"x");
^
Primary.coffee:127: circle(double,double,double,char) in Picture cannot be practical to (int,int,int,java.lang.Cord)
p.circumvolve(xxx,30,10,"o");
^
3 errors
// Written past // // description of this program // import java.io.*; import java.util.Scanner; //////////////////////////////////////////////////////////////////////////// class Picture { individual char[][]a; private int i; private int rmax; individual int cmax; private char background; private double rc; private double cc; private double radius; private char colour; //-------------------------------------------------------------------------- public Picture(int rmax, int cmax, char background) { this.rmax = rmax; this.cmax = cmax; this.background = background; this.a = new char [rmax][cmax]; for(int row = 0; row < rmax; row ++) { for(int col = 0; col < cmax; col++) { a[row][col] = groundwork; } } } //-------------------------------------------------------------------------- public Moving picture(char[][] a) { this.rmax = rmax; this.cmax = cmax; this.background = background; this.a = new char [rmax][cmax]; for(int row = 0; row < rmax; row ++) { for(int col = 0; col < cmax; col++) { a[row][col] = background; } } } //-------------------------------------------------------------------------- public Picture(double rc, double cc, double radius, char colour) { this.rc = rc; this.cc = cc; this.radius = radius; this.color = colour; for(int row = 0; row < rmax; row ++) { for(int col = 0; col < cmax; col++) { a[row][col] = color; } } } //-------------------------------------------------------------------------- public void impress() // seperate method to print the other classes { for (int row = 0; row < a.length; row++) { for (int col = 0; col < a[i].length; col++) { System.out.impress(a[row][col] + " "); } Organisation.out.println(); } } //-------------------------------------------------------------------------- public Movie rectangle(int rlo, int rhi, int clo, int chi, char color) { char[][] upshot = new char[a.length][a.length]; for(int row = 0; row < a.length; row++) { for(int col = 0; col < a[i].length; col++) { if(row >= rlo && row <= rhi) { effect[row][col] = color; } if(col >= clo && row <= chi) { consequence[row][col] = color; } } } return new Flick(result); } //-------------------------------------------------------------------------- public Film circumvolve (double rc, double cc, double radius, char color) { char[][] circle = new char[a.length][a.length]; for(int row = 0; row < a.length; row++) { for(int col = 0; col < a[i].length; col++) { double d = Math.sqrt((rc-row)*(rc-row)+(cc-col)*(cc-col)); if(d <= radius) { circle[row][col] = color; } } } return new Picture(circle); } //-------------------------------------------------------------------------- } // stop grade Flick //////////////////////////////////////////////////////////////////////////// course Hw08 { //-------------------------------------------------------------------------- public static void main ( char [][] args ) throws Exception { Film p = Picture(50,fifty," "); p.print(); p.rectangle(10,20,10,20,"x"); p.impress(); p.circle(30,xxx,ten,"o"); } //-------------------------------------------------------------------------- } // end class Hw08 //////////////////////////////////////////////////////////////////////////// #eight
Re: cartoon a filled rectangle/circle in a 2d assortment
Posted xvi October 2011 - 09:55 PM
For your mail about the constructor, this a[row][col] = background; deals with the param, not the instance variable this.a.
For your second set of errors, chars are surrounded by single quotes, and Strings by double quotes.
char 10 = 'ten'; String y = "y";
#9
Re: drawing a filled rectangle/circle in a 2d array
Posted 16 Oct 2011 - 10:10 PM
macosxnerd101, on xvi Oct 2011 - 09:55 PM, said:
For your mail service about the constructor, this a[row][col] = groundwork; deals with the param, not the instance variable this.a.
For your second prepare of errors, chars are surrounded by single quotes, and Strings past double quotes.
char x = 'x'; String y = "y";
so i fixed the second set of errors, only I'm nevertheless very confused about the constructor what do yo hateful by a[row][col] = background; deals with the param and non the instance variable? can you provide an case?
thanks for your aid and then far!
This postal service has been edited by BlahBlahMan: sixteen Oct 2011 - x:12 PM
#x
Re: cartoon a filled rectangle/circle in a second assortment
Posted 16 October 2011 - x:13 PM
Quote
public Picture(char[][] a)
This variable a is a parameter. It is local to the method. Since you lot also have an case variable named a, the parameter takes precedence in the method. So when you lot refer to a[row][col] = background;, you are modifying the parameter- a local variable. When the constructor call is completed, the instance variable volition not be modified. If you lot assign this.a[row][col] = background;, the data will be saved in the instance variable.
#eleven
Re: drawing a filled rectangle/circle in a 2d array
Posted sixteen October 2011 - 10:32 PM
macosxnerd101, on 16 October 2011 - 10:13 PM, said:
Quote
public Moving-picture show(char[][] a)
This variable a is a parameter. It is local to the method. Since you also have an case variable named a, the parameter takes precedence in the method. And so when you refer to a[row][col] = background;, you are modifying the parameter- a local variable. When the constructor call is completed, the instance variable will not be modified. If you assign this.a[row][col] = groundwork;, the information will be saved in the example variable.
I kind of get what you are saying, only how come the mistake merely pops upwards in the first constructor?
#12
Re: drawing a filled rectangle/circle in a 2nd array
Posted 16 Oct 2011 - 11:03 PM
revised code, still getting fault
Main.java:123: cannot observe symbol
symbol : method Film(int,int,char)
location: course Hw08
Flick p = Picture(50,50,' ');
^
1 fault
// Written by Steve Nham // // description of this programme // import java.io.*; import java.util.Scanner; //////////////////////////////////////////////////////////////////////////// class Picture { private char[][]a; private int i; private int rmax; private int cmax; individual char background; private double rc; private double cc; individual double radius; private char color; //-------------------------------------------------------------------------- public Movie(int rmax, int cmax, char background) { this.rmax = rmax; this.cmax = cmax; this.background = background; this.a = a; for(int row = 0; row < rmax; row ++) { for(int col = 0; col < cmax; col++) { this.a[row][col] = background; } } } //-------------------------------------------------------------------------- public Picture(char[][] a) { this.rmax = rmax; this.cmax = cmax; this.background = background; this.a = a; for(int row = 0; row < rmax; row ++) { for(int col = 0; col < cmax; col++) { this.a[row][col] = background; } } } //-------------------------------------------------------------------------- public Picture(double rc, double cc, double radius, char color) { this.rc = rc; this.cc = cc; this.radius = radius; this.colour = colour; for(int row = 0; row < rmax; row ++) { for(int col = 0; col < cmax; col++) { this.a[row][col] = color; } } } //-------------------------------------------------------------------------- public void print() { for (int row = 0; row < a.length; row++) { for (int col = 0; col < a[i].length; col++) { Arrangement.out.print(a[row][col] + " "); } System.out.println(); } } //-------------------------------------------------------------------------- public Picture show rectangle(int rlo, int rhi, int clo, int chi, char colour) { char[][] event = new char[a.length][a.length]; for(int row = 0; row < a.length; row++) { for(int col = 0; col < a[i].length; col++) { if(row >= rlo && row <= rhi) { result[row][col] = colour; } if(col >= clo && row <= chi) { result[row][col] = colour; } } } render new Picture(result); } //-------------------------------------------------------------------------- public Picture circle (double rc, double cc, double radius, char color) { char[][] circle = new char[a.length][a.length]; for(int row = 0; row < a.length; row++) { for(int col = 0; col < a[i].length; col++) { double d = Math.sqrt((rc-row)*(rc-row)+(cc-col)*(cc-col)); if(d <= radius) { circle[row][col] = color; } } } return new Motion picture(circle); } //-------------------------------------------------------------------------- } // terminate class Picture show //////////////////////////////////////////////////////////////////////////// class Hw08 { //-------------------------------------------------------------------------- public static void main ( char [][] args ) throws Exception { Picture p = Film(50,50,' '); p.print(); p.rectangle(10,20,10,20,'x'); p.print(); p.circumvolve(30,30,10,'o'); } //-------------------------------------------------------------------------- } // end class Hw08 //////////////////////////////////////////////////////////////////////////// #13
Re: drawing a filled rectangle/circumvolve in a second array
Posted 16 October 2011 - 11:23 PM
At present i get a
Exception in thread "main" java.lang.NoClassDefFoundError: Chief Caused past: java.lang.ClassNotFoundException: Main at java.cyberspace.URLClassLoader$i.run(URLClassLoader.java:200) at coffee.security.AccessController.doPrivileged(Native Method) at coffee.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:303) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) at java.lang.ClassLoader.loadClassInternal(ClassLoader.coffee:316) Could not find the main class: Chief. Program will exit.
// Written by Steve Nham // // description of this program // import java.io.*; import java.util.Scanner; //////////////////////////////////////////////////////////////////////////// course Picture { private char[][]a; private int i; private int rmax; individual int cmax; individual char background; private double rc; private double cc; private double radius; private char color; //-------------------------------------------------------------------------- public Moving picture(int rmax, int cmax, char background) { this.rmax = rmax; this.cmax = cmax; this.groundwork = background; this.a = a; for(int row = 0; row < rmax; row++) { for(int col = 0; col < cmax; col++) { this.a[row][col] = background; } } } //-------------------------------------------------------------------------- public Movie(char[][] a) { this.rmax = rmax; this.cmax = cmax; this.background = background; this.a = a; for(int row = 0; row < rmax; row++) { for(int col = 0; col < cmax; col++) { this.a[row][col] = background; } } } //-------------------------------------------------------------------------- public Picture(double rc, double cc, double radius, char color) { this.rc = rc; this.cc = cc; this.radius = radius; this.color = color; for(int row = 0; row < rmax; row++) { for(int col = 0; col < cmax; col++) { this.a[row][col] = colour; } } } //-------------------------------------------------------------------------- public void impress() { for (int row = 0; row < a.length; row++) { for (int col = 0; col < a[i].length; col++) { System.out.print(a[row][col] + " "); } Organization.out.println(); } } //-------------------------------------------------------------------------- public Picture rectangle(int rlo, int rhi, int clo, int chi, char color) { char[][] result = new char[a.length][a.length]; for(int row = 0; row < a.length; row++) { for(int col = 0; col < a[i].length; col++) { if(row >= rlo && row <= rhi) { result[row][col] = color; } if(col >= clo && row <= chi) { result[row][col] = color; } } } render new Movie(event); } //-------------------------------------------------------------------------- public Moving picture circle (double rc, double cc, double radius, char color) { char[][] circle = new char[a.length][a.length]; for(int row = 0; row < a.length; row++) { for(int col = 0; col < a[i].length; col++) { double d = Math.sqrt((rc-row)*(rc-row)+(cc-col)*(cc-col)); if(d <= radius) { circle[row][col] = color; } } } return new Picture(circle); } //-------------------------------------------------------------------------- } // end class Picture //////////////////////////////////////////////////////////////////////////// class Hw08 { //-------------------------------------------------------------------------- public static void principal ( char [][] args ) throws Exception { Motion picture p = new Film(50,l,' '); p.print(); p.rectangle(10,20,10,20,'x'); p.impress(); p.circumvolve(30,thirty,10,'o'); } //-------------------------------------------------------------------------- } // end class Hw08 //////////////////////////////////////////////////////////////////////////// #14
Re: drawing a filled rectangle/circumvolve in a 2d array
Posted 16 October 2011 - xi:30 PM
The class with main() should be public and the file should exist saved past its proper noun. And then class Hw08, is the 1 to exist public and a file name will be Hw08.coffee
#15
Re: drawing a filled rectangle/circle in a 2d array
Posted 16 Oct 2011 - eleven:33 PM
smohd, on 16 October 2011 - 11:thirty PM, said:
The class with chief() should exist public and the file should be saved by its name. So grade Hw08, is the ane to be public and a file name volition be Hw08.java
but isnt this public already? and the file name is Hw08.java
//////////////////////////////////////////////////////////////////////////// form Hw08 { //-------------------------------------------------------------------------- public static void master ( char [][] args ) throws Exception { Picture p = new Film(50,fifty,' '); p.print(); p.rectangle(ten,20,10,20,'x'); p.print(); p.circle(30,xxx,10,'o'); } //-------------------------------------------------------------------------- } // finish class Hw08 //////////////////////////////////////////////////////////////////////////// Source: https://www.dreamincode.net/forums/topic/251602-drawing-a-filled-rectanglecircle-in-a-2d-array/
0 Response to "Draw a Circle With Radius Using 2d Array in C"
Post a Comment