From: neil on 14 Jul 2010 08:58 This problem comes from Mr Glassborow's book chpt 5 exercise 6. I need to: 'Write a function to plot a point at the coordinates given by the arguments passes in to the function, but which calls the functions fom exercise 5 to determine the color and size of the square to be plotted. Note that, as for all free functions that use the playpen window, you will need to provide a parameter that is a reference to an fgw::playpen so that the caller can tell the function which fgw::playpen object is being used.' I may be a bit rusty as I have not studied this for a while. I have several problems. 1.First is that by default the origin will be in the centre of the playpen. To make it simple, I need to set the origin so it will always be at the top left corner. How to do this? 2.once the user gives x,y coordinates the function will need to test to see if the values are 'within range'. Valid values will depend on the scale given. How can I test to know that for a given scale the x,y coordinates are valid? 3. Is this a right function declaration Void drawhere(int x, int y, fgw::playpen&) ? Thanks
From: Francesco S. Carta on 14 Jul 2010 10:23 neil <invalid(a)invalid.net>, on 14/07/2010 12:58:35, wrote: > This problem comes from Mr Glassborow's book chpt 5 exercise 6. I need > to: > 'Write a function to plot a point at the coordinates given by the > arguments passes in to the function, but which calls the functions fom > exercise 5 to determine the color and size of the square to be plotted. > Note that, as for all free functions that use the playpen window, you > will need to provide a parameter that is a reference to an fgw::playpen > so that the caller can tell the function which fgw::playpen object is > being used.' > I may be a bit rusty as I have not studied this for a while. > I have several problems. > 1.First is that by default the origin will be in the centre of the > playpen. To make it simple, I need to set the origin so it will always > be at the top left corner. How to do this? Unfortunately I don't have the book you're referencing, hence I ignore the details that you didn't post here. In order to translate the reference point of a rectangle you need to add or subtract a certain amount to the coordinates: that "certain amount" depends on the size of the rectangle and the position of the old reference point. In this case the elements would be the height and width of the rectangle along with half of each of them (since the old reference point was its center). > 2.once the user gives x,y coordinates the function will need to test to > see if the values are 'within range'. Valid values will depend on the > scale given. How can I test to know that for a given scale the x,y > coordinates are valid? First bring the coordinates to the correct scale, then compare each coordinate to the allowed min and max values. If any of these tests fails then the coordinates are not valid. Note that checking for the validity of a single coordinate isn't the same as checking the validity of an entire rectangle - you need further tests in such case. As a side note, you can follow two paths after discovering that the input is invalid: either exit the function without doing anything or silently clamp the values to the closest valid point(s) and proceed drawing. > 3. Is this a right function declaration > Void drawhere(int x, int y, fgw::playpen&) ? Strictly speaking, it's not valid. To be _a_ valid declaration you need to add a semicolon at the end (omitting the question mark, as you surely know) and the correct keyword is "void" and not "Void" - yes, that's nitpicking :-) Further than that, you're correctly declaring a third parameter as a reference to playpen, but you're not giving it any identifier name (in other words, you will not be able to access that parameter within the function). Hope that helps. -- FSC - http://userscripts.org/scripts/show/59948 http://fscode.altervista.org - http://sardinias.com
From: Francis Glassborow on 14 Jul 2010 10:39 neil wrote: > This problem comes from Mr Glassborow's book chpt 5 exercise 6. Which book please? I need > to: > 'Write a function to plot a point at the coordinates given by the > arguments passes in to the function, but which calls the functions fom > exercise 5 to determine the color and size of the square to be plotted. > Note that, as for all free functions that use the playpen window, you > will need to provide a parameter that is a reference to an fgw::playpen > so that the caller can tell the function which fgw::playpen object is > being used.' > I may be a bit rusty as I have not studied this for a while. > I have several problems. > 1.First is that by default the origin will be in the centre of the > playpen. To make it simple, I need to set the origin so it will always > be at the top left corner. How to do this? by using the appropriate overload of the origin member-function: Playpen has an overloaded pair of member functions origin() to handle the graphical origin. The one that has no parameters returns a special nested type, playpen::origin_data, to package the x and y co-ordinates of the current origin referred to the top left of the playpen window. The second origin() member function has two int parameters which are used as the window co-ordinates for a new location for the origin used by Playpen functions. It returns a reference to the playpen object using it. > 2.once the user gives x,y coordinates the function will need to test to > see if the values are 'within range'. Valid values will depend on the > scale given. How can I test to know that for a given scale the x,y > coordinates are valid? Well if you know the origin and no the dimensions of playpen in raw pixels (512 by 512 if memory serves me correctly) Multiply each co-ordinate by the scale and you will have the offset from the origin in raw pixels. Now if you know where the origin is you should be able to compute whether the point is in the display window. > 3. Is this a right function declaration > Void drawhere(int x, int y, fgw::playpen&) ? if you spell void with a lower case 'v' it will be a declaration, whether it is the one you want depends on exactly what you are trying to do. > > Thanks -- Note that robinton.demon.co.uk addresses are no longer valid.
From: Francis Glassborow on 14 Jul 2010 10:47 neil wrote: > This problem comes from Mr Glassborow's book chpt 5 exercise 6. I need > to: > 'Write a function to plot a point at the coordinates given by the > arguments passes in to the function, but which calls the functions fom > exercise 5 to determine the color and size of the square to be plotted. > Note that, as for all free functions that use the playpen window, you > will need to provide a parameter that is a reference to an fgw::playpen > so that the caller can tell the function which fgw::playpen object is > being used.' > I may be a bit rusty as I have not studied this for a while. > I have several problems. > 1.First is that by default the origin will be in the centre of the > playpen. To make it simple, I need to set the origin so it will always > be at the top left corner. How to do this? > 2.once the user gives x,y coordinates the function will need to test to > see if the values are 'within range'. Valid values will depend on the > scale given. How can I test to know that for a given scale the x,y > coordinates are valid? > 3. Is this a right function declaration > Void drawhere(int x, int y, fgw::playpen&) ? > > Thanks Moving the origin to the top left of the screen may cause you problems because Playpen uses the conventional directions for co-ordinates (positive is measured to the right and upwards). This means that with the origin at the top left all displayable points will have negative y co-ordinates. You might find it easier to shift the origin to the bottom right. However I am not sure why you think this will make things easier. I would suggest that you experiment a bit with moving the origin and plotting points to get a feel for how your co-ordinate data maps to the display.
From: neil on 15 Jul 2010 12:16 Hello, just to make my question clerer to you i have written up my program: // driver program and functions color, scale and where #include <iostream> #include <exception> #include "playpen.h" using namespace std; void where(int x, int y, fgw::playpen& paper); int color(); int scale(); int main() { try{ cout << "This program will obtain user input for color size and location of a square to be drawn\n"; fgw::playpen paper; // calls to color and scale are made from within where() int xco(0); int yco(0); cout << "Please input the X co ordinate \n"; cin >> xco; cout << "Please input the Y co ordinate \n"; cin >> yco; clog << "Calling function where() \n"; where(xco, yco, paper); // all work is done in the function. No value is returned to caller cin.get(); clog << "Function where() has finished \n"; } catch(...){ cerr << "***An Exception was thrown***\n"; } } // function definitions // color() int color() { int passback(0); int mycolor(0); // test to make sure my color is in range if it is // less than zero or if it is greater than 255 its out of range bool test(false); do{ cout << "Input a number 0 to 255 for color \n"; cin >> mycolor; if (mycolor >= 0) test = false; if (mycolor <= 255) test = false; if (mycolor < 0) { // out of range ask for another value cout << "This is out of range. Please input a value between 0 and 255 \n"; test = true; } if (mycolor > 255) { // out of range ask for another value cout << "This is out of range. Please input a value between 0 and 255 \n"; test = true; } } while(test == true); test = false; if (test == false) passback = mycolor; return mycolor; } // scale() int scale() { int passback(0); int myscale(0); // test to make sure myscale is in range // if it is less than 1 or greater than 64 oor bool test(false); do{ cout << "Input a number 1 to 64 for scale \n"; cin >> myscale; if (myscale >= 1) test = false; if (myscale <= 64) test = false; if (myscale < 1) { // out of range cout << "This is out of range. Please input a value between 1 and 64 \n"; test = true; } if (myscale > 64) { // out of range cout << "This is out of range. Please input a value between 1 and 64 \n"; test = true; } }while(test == true); test = false; if(test == false) passback = myscale; return myscale; } // where() void where(int x, int y, fgw::playpen& paper) { // what color? int thiscolor(0); thiscolor = color(); // what scale? int thisscale(0); thisscale = scale(); paper.scale(thisscale); // plot where? //paper.origin(50,50) paper.plot(x,y,thiscolor); paper.display(); cout << "Press ENTER to end Program \n"; cin.get(); } The problem is with the function where(). The program works but if an out of range value id given for x,y - nothing will be out put. I would really like to identify out of range values like i have done in the previous two functions in here - although the replies have explained in a way how i might do this, i dont really understand and would ask that someone can please tell me how to check that the values x,y are in range - which depend upon the scale. thanks
|
Next
|
Last
Pages: 1 2 Prev: Discount ED Hardy Coat Kid's Next: calling a function within another function |