why is height and width when declared as a constant gives the default value? - max

When I use the height and width as a constant I was wondering why it's giving me the value 100? Is it because size(); was not declared above? How do I set it to the size of the canvas? Because the following prints 100
Any help would do!!
Here is a copy of my code:
final int SIZE = height;
void setup() {
size (1000,1000);
println(SIZE);
}

For primitive types the value itself is stored in a variable.
So when you call final int SIZE = height; SIZE will have the value which height had immediately prior to that assignment. All subsequent changes to height will not affect SIZE.

The width and height variables are set when you call the size() function. You're using them outside of a function, which means they're happening before the size() function is called, which is why they still have their default values.
To fix this, you need to move the variable initialization to be after size() is called:
int SIZE;
void setup() {
size (1000,1000);
SIZE = height
println(SIZE);
}

See the processing documentation for size():
... If size() is not used, the window will be given a default size of 100 x 100 pixels.
and height:
... The value of height defaults to 100 if size() is not used in a program.
This means as long size() was not called, the values for height and width are initialized to 100.
If you read the variables before, as you do it, then they will return 100.
You have to initialize SIZE after calling size():
void setup() {
size (1000,1000);
SIZE = height;
}

Related

FLTK Group minimum size

With FLTK (version 1.4.0), is it possible to set minimum size for an Fl_Group widget? Either explicitly or automatically so that it wouldn't resize smaller than needed to display all its children? The Fl_Window class has method size_range which allows to set the smallest window size, however Fl_Group doesn't have such a method.
If this is not available, then maybe there are some other way to enforce a constraint on how small a non-window group widget can be?
It is possible to partly achieve this by using the size_range method of the top-most window, however if we use Fl_Tile than each tile would not be constrained in any way. Yes, an Fl_Box widget inside an Fl_Tile can limit minimal sizes of the outer tiles, but it's not exactly what I need here.
You can override the resize method of an Fl_Group widget. The resize method is triggered automatically when resizing even if not called explicitly.
#include <FL/Enumerations.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Group.H>
#include <FL/Fl.H>
struct MyGroup : public Fl_Group {
MyGroup(int x, int y, int w, int h, const char *label = 0): Fl_Group(x, y, w, h, label) {
box(FL_ENGRAVED_FRAME);
}
virtual void resize(int x, int y, int w, int h) override {
Fl_Group::resize(x, y, w, h);
if (w < 300 || h < 200) {
resize(x, y, 300, 200);
}
}
};
int main() {
Fl_Double_Window w(400, 300);
MyGroup g(20, 20, 360, 260);
w.end();
w.resizable(&w);
w.show();
return Fl::run();
}
You will notice that the window is resizable, and if you try to decrease its size the group widget will get smaller to a certain point only.

Make an object MAX_size at the bottom of eh screen and MIN_size at the top of the screen using processing

final int MAX_SIZE = 500;
final int MIN_SIZE = 50;
void setup (){
size(500,500);
}
void draw(){
setSizeXAndY();
}
void setSizeXAndY(){
size= (MAX_SIZE*mouseY)/(width);
}
I need help with the arithmetic please for the size function please.
The code as you posted it makes very little sense in it's current form and has a syntax error (size isn't declared in setSizeXAndY() although you try to assign a value to it)
Unfortunately it doesn't communicate your intentions clearly.
The (misspelled) title provides more information but is also misleading: by object do you mean an instance of a class or a visual object to rendered on screen (e.g. rectangle/ellipse/etc.) ?
Your sketch is 500x500 pixels so you already have MAX_SIZE at the bottom (as you set it equal to 500). MIN_SIZE should be equal to 0 to be at the top. (e.g. final int MIN_SIZE = 50;)
Regarding arithmetic for the size function I guess you mean this:
size= (MAX_SIZE*mouseY)/(width);
I'm not sure this does what you expect to do, but you can do a bit of basic debugging and print your values out:
void setSizeXAndY(){
int size = (MAX_SIZE*mouseY)/(width);
println("MAX_SIZE",MAX_SIZE,"mouseY",mouseY,"width",width,size);
}
You'll notice a pattern:
MAX_SIZE 500 mouseY 258 width 500 258
MAX_SIZE 500 mouseY 258 width 500 258
MAX_SIZE 500 mouseY 258 width 500 258
MAX_SIZE 500 mouseY 258 width 500 258
mouseY is the same as size !
That is because you're sketch is 500x500 so 500 x mouseY / 500 = mouseX (remember multiplication is commutative, you can change the order of the factors and the result will remain the same)
Maybe you mean inverting Y ? If so, you can simply subtract the Y value from the maximum Y value. Here's an example based on your code:
final int MAX_SIZE = 500;
final int MIN_SIZE = 50;
void setup () {
size(500, 500);
}
void draw() {
background(0);
setSizeXAndY();
}
void setSizeXAndY() {
int size = (MAX_SIZE*mouseY)/(width);
println("MAX_SIZE", MAX_SIZE, "mouseY", mouseY, "width", width, size);
// subtract value from it's maximum value "flips/mirrors" the value
int invertedY = MAX_SIZE - mouseY;
// test: draw the regular mouseY in green
fill(0,192,0);
ellipse(mouseX, mouseY, 18, 18);
// test: draw the inverted mouseY in red
fill(192,0,0);
ellipse(mouseX, invertedY, 18, 18);
}
Alternatively you can use map() :
final int MAX_SIZE = 500;
final int MIN_SIZE = 50;
void setup () {
size(500, 500);
}
void draw() {
background(0);
setSizeXAndY();
}
void setSizeXAndY() {
int size = (MAX_SIZE*mouseY)/(width);
println("MAX_SIZE", MAX_SIZE, "mouseY", mouseY, "width", width, size);
// subtract value from it's maximum value "flips/mirrors" the value
float invertedY = map(mouseY,0,MAX_SIZE,MAX_SIZE,0);
// test: draw the regular mouseY in green
fill(0,192,0);
ellipse(mouseX, mouseY, 18, 18);
// test: draw the inverted mouseY in red
fill(192,0,0);
ellipse(mouseX, invertedY, 18, 18);
}
but it's worth learning and getting the hand of using basic arithmetic.
Learning to program can be frustrating at first but take your time with it, be patient and keep trying over and over again: you will eventually get it !
Take it one step at a time and make sure you fully understand a notion before moving to the next: understanding the fundamentals (variables (primitive types) / conditions / loops / arrays / functions ) is crucial. If you get these right, everything else will much much easier.
Try to write code everyday for at least half an hour / an hour outside of your homework: something fun you'd like to try.

How to convert dp to px in xamarin.android?

I want to convert dp to px in my C# code in xamarin.android, but all I could find were java codes in android studio that have some problems in xamarin. I tried to use equivalent like using Resources instead of getResources() and I could solve some little problems, but there are some problems yet that I couldn't find any equivalent for them. here are original codes, my codes, and my codes problems in xamarin:
First code
(found from Programatically set height on LayoutParams as density-independent pixels)
java code
int height = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, < HEIGHT >, getResources().getDisplayMetrics());
C# code
int height = (int)TypedValue.ApplyDimension(TypedValue.COMPLEX_UNIT_DIP, < HEIGHT >, Resources.DisplayMetrics);
problems:
'TypedValue' does not contain a definition for 'COMPLEX_UNIT_DIP'
Invalid expression term < (The same error for >)
The name 'HEIGHT' does not exist in the current context
Second code
(found from Formula px to dp, dp to px android)
java code
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
C# code
DisplayMetrics displayMetrics = Application.Context.Resources.DisplayMetrics;
int pixel = Math.Round(dp * (displayMetrics.Xdpi / DisplayMetrics.DensityDefault));
problem
Operator '/' cannot be applied to operands of type 'float' and 'DisplayMetricsDensity'
Now I have actually two questions. Which code is more proper? What's equivalent code for them in xamarin.android?
Thanks in advance.
Solution to "First code":
Xamarin tends to move constants into their own enums. COMPLEX_UNIT_DIP can be found on ComplexUnitType enum. Also you cannot have < HEIGHT > in your code you actually need to pass in dips to get the equivalent pixel value. in the example below I am getting pixels in 100 dips.
var dp = 100;
int pixel = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip, dp, Context.Resources.DisplayMetrics);
Solution to "Second code":
You need to explicitly cast 'DisplayMetrics.DensityDefault' to a float and the entire round to an int:
int pixel = (int)System.Math.Round(dp * (displayMetrics.Xdpi / (float)DisplayMetrics.DensityDefault));
I prefer the first approach as the second code is specifically for calculating along the "x dimension":
Per Android docs and Xamarin.Android docs, the Xdpi property is
"The exact physical pixels per inch of the screen in the X dimension."
These are from the project that I am currently working on..
public static float pxFromDp(Context context, float dp)
{
return dp * context.Resources.DisplayMetrics.Density;
}
public static float dpFromPx(Context context, float px)
{
return px / context.Resources.DisplayMetrics.Density;
}
From android source code textview settextsize
var convertToDp = TypedValue.ApplyDimension( ComplexUnitType.Dip, size, context.Resouces.DisplayMetrics);
dp to px:
DisplayMetrics displayMetrics = Application.Context.Resources.DisplayMetrics;
double pixel = Math.Round((dp * DisplayMetrics.DensityDefault) + 0.5);
Answer taken from here : https://stackoverflow.com/a/8490361/6949388
Sorry, not enough rep to comment.

Set minimum and max range in relation to window size

final float MAX_SIZE = 35000;
final float MIN_SIZE = MAX_SIZE/10;
float alienSize;
float alienY;
float alienX;
void draw()
{
alienX = mouseX;
alienY = mouseY;
alienSize = MAX_SIZE*mouseY/height;
}
My goal is to make the variable alienSize be the max size when it ( and the mouse ) is at the bottom of the window and 1/10th the size when it ( and the mouse ) is at the top of the window. I have so far this code which works for the max size but there is no limit to how small the alien can get. any help please :)
Edit: I have to hand this in so no cheating please like having an if statement check for the size or anything
You could use the map() function. The map() function takes 5 values:
An input value.
A minimum input value.
A maximum input value.
A minimum output value.
A maximum output value.
It then returns an output value that's based on the relative position of the input value, scaled to the output min and max. For example, if you call map(150, 100, 200, 500, 700) then you'll get back a value of 600 because 150 is halfway between 100 and 200, so it figures out what value is halfway between 500 and 700.
Here's an example:
float minSize = 50;
float maxSize = 100;
void setup(){
size(500, 100);
}
void draw(){
float size = map(mouseX, 0, width, minSize, maxSize);
background(0);
ellipse(mouseX, height/2, size, size);
}
More info can be found in the reference.

Multiplying width or height by a number yields 0 in Processing

In Processing, I keep getting 0 when I multiply width or height by a number. For example:
int x = width*2;
I get x = 0. Why?
When you define a size in processing, width and height are automatically assigned (example size(500,500)). width and height are 500 in this case.
Note that width and height only have values after you call the size() function. (Or if you don't call the size() function, after setup() is called.)
That can be a problem if you do stuff before calling the size() function, such as at the beginning of your sketch:
//this happens before the size() function is called!
int x = width*2;
void setup(){
size(500, 500);
}
The solution is to put the declaration of sketch-level variables at the top of your sketch, but only do their initialization after calling the size() function:
int x;
void setup(){
size(500, 500);
x = width*2;
}
This could also be caused if you try to define your own width and height variables:
//don't do this!
int width;
int height;
void setup(){
size(500, 500);
println(width);
}
Don't do this! Just use the width and height variables that Processing has already declared for you.
More generally, as you've seen, questions tend to be closed pretty quickly if people can't reproduce your problem. Although you did include the fact that you're using Processing, you'll have better luck in the future if you include an MCVE that we can run to see the problem ourselves. The code I've posted in this answer is a good example of the type of thing you should include in your questions in the future, that way they don't get closed.

Resources