when generating a number, only gives 0 - android

when i generate a problem or number in this case, it seems to always come out as 0, when it should be generating a number between 0 and the value AD given. which in this case is 0 through 11, but only generates a 0 every time. for some reason the AD int is returning as 1 when i log it to the debug console. any help?
Double d = Math.random() * Max1;
O1 = d.intValue();
Log.d("BasicsPractice.java","the GenerateOperation function generated : " + O1 + " : as the number to determin the operation to be built");
if(O1 == 0) {
if(AddP == true) {
int AD = DM.AddDiff + 1;
Double d1 = Math.random() * AD;
A1 = d1.intValue();
Log.d("BasicsPractice.java","the first Addition number generated was: " + A1);
Double d5 = Math.random() * AD;
A2 = d5.intValue();
Log.d("BasicsPractice.java","the second Addition number generated was: " + A2);
Output = A1 + " + " + A2;
Log.d("BasicsPractice.java","the outputted problem was: " + Output);
OutputToProblemView();
}

Math.random() returns a number from 0.0 to 1.0. So you need to convert it into int.
Try this out.
Math.floor(Math.random()*12);
This will generate numbers between 0 to 11.

Related

Example pointclouds with ARCore

Can I get some examples about pointclouds with ARCore? I really search it for days.
Currently I am working on an application similar to this one:This app
Has the feature to view pcl and save files in .ply format
Thanks
The HelloARSample app renders pointcloud in a scene. You can get the coordinates for each point and save them manually in a .ply format.
To get a point cloud, you can create a float buffer and add the points from each frame.
FloatBuffer myPointCloud = FloatBuffer.allocate(capacity);
Session session = new Session(context);
Frame frame = session.update();
try (PointCloud ptcld = frame.acquirePointCloud()) {
myPointCloud.put(ptcld.getPoints());
}
The float buffer saves the points like [x1,x1,z1,confidence1, x2,x2,z2,confidence2, ...].
I havent looked at .ply file struckture, but if you want to save it to a .pcd file, you must create a header, then insert a point per line. Here is a detailed explanation on how to do it.
I did it like this
private boolean savePointCloudFile() {
String data = "";
String fileName = "pointCloud";
int points = 0;
String holder = "";
// Write the point cloud data by iterating over each point:
for (int i=0; i<pointCloud.position(); i+=4) {
data += pointCloud.get(i) + " " + // x
pointCloud.get(i + 1) + " " + // y
pointCloud.get(i + 2) + " " + // z
pointCloud.get(i + 3) + "\n"; // confidence
points = i;
}
points = points / 4 - 10; // Removed last 10 points to prevent errors in case that I lost points
// Write file header
data = "# .PCD v.7 - Point Cloud Data file format\n" +
"VERSION .7\n" +
"FIELDS x y z rgb\n" + // confidence represented by rgb
"SIZE 4 4 4 4\n" + // you only have 3 values xyz
"TYPE F F F F\n" + // all floats
"COUNT 1 1 1 1\n" +
"WIDTH " + points + "\n" +
"HEIGHT 1\n" +
"VIEWPOINT 0 0 0 1 0 0 0\n" +
"POINTS " + points + "\n" +
"DATA ascii \n" + data;
//BufferedWriter out = new BufferedWriter(new FileWriter(new File(new File(Environment.getExternalStoragePublicDirectory(
// Environment.DIRECTORY_DOCUMENTS), fileName + ".pcd"));
try {
File file = new File(context.getExternalFilesDir(null), fileName +".pcd");
FileOutputStream stream = new FileOutputStream(file);
file.createNewFile();
//FileOutputStream out = new FileOutputStream(file);
stream.write(data.getBytes());
stream.close();
Log.i("SUCCESS", "File saved successfully in " + file.getAbsolutePath());
return true;
} catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
return false;
}
}
You should save the file from within a separate thread as it may cause a timeout error, because it takes too long to save so many points to a file.
You should get a file similar to this
# .PCD v.7 - Point Cloud Data file format
VERSION .7
FIELDS x y z rgb
SIZE 4 4 4 4
TYPE F F F F
COUNT 1 1 1 1
WIDTH 3784
HEIGHT 1
VIEWPOINT 0 0 0 1 0 0 0
POINTS 3784
DATA ascii
0.068493545 -0.18897545 -0.6662081 0.007968704
0.26833203 -0.18425867 -1.5039357 0.02365576
0.19286658 -0.2141684 -1.58289 0.038087178
0.070703566 -0.17931458 -0.69418937 0.016636848
0.044586033 -0.18726173 -0.6926071 0.024707714
0.04002113 -0.20350328 -0.68689686 0.018577512
0.029185327 -0.18594348 -0.73340106 0.12292312
0.0027626567 -0.20299685 -1.5578543 0.15424652
-0.031320766 -0.20478198 -0.70128816 0.13745676
-0.06351853 -0.20185146 -0.61755043 0.15234329
-0.08655308 -0.19128543 -0.6776818 0.170851
1.0159657 -0.41043654 -6.8713074 0.05946503
-0.031778865 -0.20536968 -1.5218562 0.15976532
-0.09223208 -0.19543779 -0.61643535 0.12331226
0.02384475 -0.20319816 -1.7497014 0.15273231
-0.10013421 -0.19931296 -0.5924832 0.16186734
0.49137634 -0.09052197 -5.7263794 0.16080469
To viaualize the point cloud you can use pcl_viewer or Matlab. In Matlab I just typed
ptCloud = pcread('pointCloud.pcd');
pcshow(ptCloud);

Label doesn’t print integer value received from server in cocos2dx

I am getting json data from server like this -
if (response->isSucceed()) {
log("Get success");
std::vector<char>* buffer = response->getResponseData();
std::string res;
res.insert(res.begin(), buffer->begin(), buffer->end());
log("res: %s", res.c_str());
rapidjson::Document d;
d.Parse<0>(res.c_str());
if(d.IsObject()){
log("true"); // print the value of obtaining hello
}
F = d["F"].GetString();
W = d["W"].GetString();
T = d["T"].GetString();
Fp = d["Fp"].GetString();
Wp = d["Wp"].GetString();
Tp = d["Tp"].GetString();
}
Now i want to display that value of “F” using label…Value of F is integer number.
So i do this -
foodnumber = Label::createWithTTF(F, "fonts/A little sunshine.ttf", 40 );
foodnumber->setColor(Color3B(109,61,23));
foodnumber->setPosition( Vec2(foodtitle->getPositionX() + foodtitle-
>getContentSize().width/4 + origin.x, foodtitle->getPositionY() + origin.y)
);
this->addChild( foodnumber);
It doesn’t print the value…
The value in the F is Number.
But when i log the value, it gets logged right, but it doesn’t get displayed on app as label.
Thank you

Android GraphView incorrect X-axis steps when formatting labels

I am using android-graphview to plot Temps with time for a weather forecasting app.
In my tab "24hours" I plot the data from the current time and until 24hours later. I have weather forecasts in 3-hour steps.
To achieve proper display on x-axis, first I have to keep the hours additively when the day changes. So if current time is 18:00 to plot the data until tommorow 18:00, then x-axis values I plot are --> 18 21 24 27 30 33 36 39 42.
Next, I use the label formatter to change the x-axis Labels to
--> 18 21 0 3 6 9 12 15 18.
The graph is displayed correctly at the correct points, however the labels follow a step of 2 instead of 3 but the range is correct.
System.out.println("WHAT1? " .. shows the input data point pairs correctly with hours increasing in steps of 3, but
System.out.println("pout " .. and "pour" show the x-axis values increase in steps of 2 (although being at the correct positions).
How can I have the labels be displayed at the 3hour step?
What am I doing wrong? Here is the code:
DataPoint b= new DataPoint(0,0);//test
DataPoint[] point_array= new DataPoint[9];
String[] time_label_24=new String[9];
GraphView graph = (GraphView) findViewById(R.id.graph);
hour=Integer.parseInt(Lista.get(x)[15]);
for(int i=x;i<=x+8;i++){
if((i-x)<Lista.size()) {
b = new DataPoint(hour, Float.parseFloat(Lista.get(i)[4]));
point_array[i - x] = b;
System.out.println("WHAT1? " + (i - x) + " " + point_array[i - x] + " " + hour);
time_label_24[i-x]=Lista.get(i)[15];
hour = hour + 3;
}
}
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(point_array);
graph.addSeries(series);
h=Integer.parseInt(Lista.get(x)[15]);
graph.getViewport().setXAxisBoundsManual(true);
//graph.getViewport().setMinX(Integer.parseInt(Lista.get(x)[15])-1);
//graph.getViewport().setMaxX(Integer.parseInt(Lista.get(x)[15])+8*3+1);
graph.getViewport().setMinX(Integer.parseInt(Lista.get(x)[15]));
graph.getViewport().setMaxX(Integer.parseInt(Lista.get(x)[15])+8*3);
graph.getGridLabelRenderer().setNumHorizontalLabels(9); //9
graph.getGridLabelRenderer().setTextSize(12f);
graph.getGridLabelRenderer().reloadStyles();
graph.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter() {
#Override
public String formatLabel(double value, boolean isValueX) {
int xylabel;
if (isValueX) {
if (value < 24) {
System.out.println("pout "+value+" " + String.valueOf(value));
xylabel = (int) Math.round(value);
return String.valueOf(xylabel);
} else{
System.out.println("pour "+value+" " + String.valueOf(value%24));
xylabel = (int) Math.round(value);
return String.valueOf(xylabel%24);
}
}else {
xylabel = (int) Math.round(value);
return String.valueOf(xylabel); // let graphview generate Y-axis label for us
}
}
});
////////
hour=Integer.parseInt(Lista.get(x)[15]);
for(int i=x;i<=x+8;i++){ // Same process for Temp FEEL
if((i-x)<Lista.size()) {
b = new DataPoint(hour, Float.parseFloat(Lista.get(i)[13]));
point_array[i - x] = b;
hour = hour + 3;
time_label_24[i-x]=Lista.get(i)[15];
System.out.println("What2? " + point_array[i - x]);
}
}
LineGraphSeries<DataPoint> series2 = new LineGraphSeries<DataPoint>(point_array);
graph.addSeries(series2);
series.setTitle("Temp");
series2.setTitle("Feel");
series.setColor(Color.WHITE);
series2.setColor(Color.RED);
graph.getLegendRenderer().setVisible(true);
graph.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP);
Here is an image of the graph:
resulting graph
try to disable human rounding via
graph.getGridLabelRenderer().setHumanRounding(false);

Unable to make backspace button function

I am adding a backspace button in my calculator app and it is working fine also. The problem is, by default, my calculator is taking digits from decimal part i.e. initially it is 0.00, when I input 1 it becomes 0.01,when I input 2 it becomes 0.12 and so on and so forth. Now, when I press backspace it is deleting 2 and showing 0.01 but if I press 3 instead of showing 0.13 it shows 1.23. How to resolve this?
Code for the backspace button:-
private String addCurrency(String digits) {
String string = ""; // Your currency
enteredNumber = enteredNumber + digits;
// Amount length greater than 2 means we need to add a decimal point
if (enteredNumber.length() > 2) {
String rupee = enteredNumber.substring(0,
enteredNumber.length() - 2); // Pound part
String paise = enteredNumber.substring(enteredNumber.length() - 2); // Pence
// part
if (enteredNumber.contains(".")) {
}
string += rupee + "." + paise;
} else if (enteredNumber.length() == 1) {
string += "0.0" + enteredNumber;
Log.d("TextWatcher", "length 1 " + string);
} else if (enteredNumber.length() == 2) {
string += "0." + enteredNumber;
Log.d("TextWatcher", "length 2 " + string);
}
return string;
}
WHERE:-
enteredNumber is just a String type variable.
If you wish to show your numbers with two decimal points all the time, then why so much trouble? Use this -
public void getMyNumber(String yourInputNumber){
Double result = Integer.ParseInt(yourInputNumber)/100;
private static DecimalFormat REAL_FORMATTER = new DecimalFormat("0.##");
textview1.setText(REAL_FORMATTER.format(result));
}

Displaying values to a TextView

hi i have problem in displaying a value into my TextView..
For example i will input 1,2,3,4 then i like to display the output in this manner in my TextView..How can i do that? please help me, thank you in advance
1 appeared 1 times
2 appeared 1 times
3 appeared 1 times
4 appeared 1 times
here's my code:
String []values = ( sum.getText().toString().split(","));
double[] convertedValues = new double[values.length];
Arrays.sort(convertedValues);
int i=0;
int c=0;
while(i<values.length-1){
while(values[i]==values[i+1]){
c++;
i++;
}
table.setText(values[i] + " appeared " + c + " times");
c=1;
i++;
if(i==values.length-1)
table.setText(values[i] + " appeared " + c + " times");
Make your textView to support multipleLines and after that create in code a StringBuffer and append to it the results, something like
resultString.append(result).append(" appeared").append(c).append(" times\n");
after that you set text for textView like:
textView.setText(resultString.toString());
Here is the idea :
// this is test string, you can read it from your textView
String []values = ( "2, 1, 3, 5, 1, 2".toString().split(","));
int [] intValues = new int[values.length];
// convert string values to int
for (int i = 0; i < values.length; ++i) {
intValues[i] = Integer.parseInt(values[i].trim());
}
// sort integer array
Arrays.sort(intValues);
StringBuilder output = new StringBuilder();
// iterate and count occurrences
int count = 1;
// you don't need internal loop, one loop is enough
for (int i = 0; i < intValues.length; ++i) {
if (i == intValues.length - 1 || intValues[i] != intValues[i + 1]) {
// we found end of "equal" sequence
output.append(intValues[i] + " appeared " + count + " times\n");
count = 1; // reset count
} else {
count++; // continue till we count all equal values
}
}
System.out.println(output.toString()); // prints what you extected
table.setText(output.toString()); // display output

Categories

Resources