Error in converting height from centimeters to feet inches - android

I write a method to convert height from centimeters to feet inches on click of a toggle button but when I try to click toggle button after entering the height in centimeter its throwing me error
java.lang.NumberFormatException: Invalid int: ""
Here is my code:
private void convertTofeetInches(EditText height_cm){
String str = height_cm.getText().toString();
int feet = (int) Math.floor(Integer.parseInt(str)/30.48);
int inches = (int)Math.round((Double.parseDouble(str)/2.54) - ((int)feet * 12));
Log.d("feet",String.valueOf(feet));
Log.d("inches",String.valueOf(inches));
enter_height.setText(""+feet + "'" +inches + "\"");
}
I think I am doing some mistake in type conversion. Can anybody please point it.

Try this,
private void convertTofeetInches(String str) throws NumberFormatException{
Double value = new Double(str);
int feet = (int) Math.floor(value / 30.48);
int inches = (int) Math.round((value / 2.54) - ((int) feet * 12));
String ouput = feet + "' " + inches + "\"";
enter_height.setText(ouput);
}

Related

Android int/double calculation randomly overflow

int n1 = 65535;
int n2 = 1659;
int fs1 = 144000;
int fs2 = 7056000;
int dfrq = 44100;
int delay = (int) ((double) n1 / 2 / ((double) fs1 / dfrq) + (double) n2 / 2 / ((double) fs2 / dfrq));
The above calculation result give me randomly values, sometimes it's correct delay= 10040, but sometimes the delay= 2147483647, which is 0b1111111111111111111111111111111 in binary, it's overflow.
The problem is why on android, this calculation gives two different results, and randomly.
My temp solution is:
double s1 = 1f * n1 / 2;
double s2 = 1f * fs1 / dfrq;
double s3 = 1f * n2 / 2;
double s4 = 1f * fs2 / dfrq;
L.d(TAG, "s1:" + s1 + " s2:" + s2 + " s3:" + s3 + " s4:" + s4);
int delay = (int) (s1 / s2 + s3 / s4);

Android Canvas: Get Y position of drawn line in canvas [duplicate]

I'd like to calculate a point on a quadratic curve. To use it with the canvas element of HTML5.
When I use the quadraticCurveTo() function in JavaScript, I have a source point, a target point and a control point.
How can I calculate a point on the created quadratic curve at let's say t=0.5 with "only" knowing this three points?
Use the quadratic Bézier formula, found, for instance, on the Wikipedia page for Bézier Curves:
In pseudo-code, that's
t = 0.5; // given example value
x = (1 - t) * (1 - t) * p[0].x + 2 * (1 - t) * t * p[1].x + t * t * p[2].x;
y = (1 - t) * (1 - t) * p[0].y + 2 * (1 - t) * t * p[1].y + t * t * p[2].y;
p[0] is the start point, p[1] is the control point, and p[2] is the end point. t is the parameter, which goes from 0 to 1.
In case somebody needs the cubic form:
//B(t) = (1-t)**3 p0 + 3(1 - t)**2 t P1 + 3(1-t)t**2 P2 + t**3 P3
x = (1-t)*(1-t)*(1-t)*p0x + 3*(1-t)*(1-t)*t*p1x + 3*(1-t)*t*t*p2x + t*t*t*p3x;
y = (1-t)*(1-t)*(1-t)*p0y + 3*(1-t)*(1-t)*t*p1y + 3*(1-t)*t*t*p2y + t*t*t*p3y;
I created this demo :
// x = a * (1-t)³ + b * 3 * (1-t)²t + c * 3 * (1-t)t² + d * t³
//------------------------------------------------------------
// x = a - 3at + 3at² - at³
// + 3bt - 6bt² + 3bt³
// + 3ct² - 3ct³
// + dt³
//--------------------------------
// x = - at³ + 3bt³ - 3ct³ + dt³
// + 3at² - 6bt² + 3ct²
// - 3at + 3bt
// + a
//--------------------------------
// 0 = t³ (-a+3b-3c+d) + => A
// t² (3a-6b+3c) + => B
// t (-3a+3b) + => c
// a - x => D
//--------------------------------
var A = d - 3*c + 3*b - a,
B = 3*c - 6*b + 3*a,
C = 3*b - 3*a,
D = a-x;
// So we need to solve At³ + Bt² + Ct + D = 0
Full example here
may help someone.
I edited talkhabis answer (cubic curve) so the curve is displayed with the right coordinates. (Couldn't comment)
The Y-coordinates needed to be changed (-p[].y+150). (A new variable for that might be a nicer and more efficient solution, but you get the idea)
// Apply points to SVG and create the curve and controllers :
var path = document.getElementById('path'),
ctrl1 = document.getElementById('ctrl1'),
ctrl2 = document.getElementById('ctrl2'),
D = 'M ' + p0.x + ' ' + (-p0.y+150) +
'C ' + c0.x + ' ' + (-c0.y+150) +', ' + c1.x + ' ' + (-c1.y+150) + ', ' + p1.x + ' ' + (-p1.y+150);
path.setAttribute('d',D);
ctrl1.setAttribute('d','M'+p0.x+','+(-p0.y+150)+'L'+c0.x+','+(-c0.y+150));
ctrl2.setAttribute('d','M'+p1.x+','+(-p1.y+150)+'L'+c1.x+','+(-c1.y+150));
// Lets test the "Bezier Function"
var t = 0, point = document.getElementById('point');
setInterval(function(){
var p = Bezier(p0,c0,c1,p1,t);
point.setAttribute('cx',p.x);
point.setAttribute('cy',-p.y+150);
t += 0.01;
if(t>=1) t=0;
},50);
// OK ... Now tring to get "y" on cruve based on mouse "x" :
var svg = document.getElementById('svg'),
point2 = document.getElementById('point2');
svg.onmousemove = function(e){
var x = (e.pageX - 50)/2,
y = (e.pageY - 50)/2;
// "-50" because of "50px margin" on the left side
// and "/2" because the svg width is 300 units and 600 px => 300 = 600/2
// Get the x,y by mouse x
var p = YBX(p0,c0,c1,p1,x);
point2.setAttribute('cx',p.x);
point2.setAttribute('cy',-p.y+150);
}
http://jsfiddle.net/u214gco8/1/
I also created some C-Code to test the results for the cubic curve. Just enter the X and Y coordinates in the main function.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void bezierCurve(int x[] , int y[])
{
double xu = 0.0 , yu = 0.0 , u = 0.0 ;
int i = 0 ;
for(u = 0.0 ; u <= 1.0 ; u += 0.05)
{
xu = pow(1-u,3)*x[0]+3*u*pow(1-u,2)*x[1]+3*pow(u,2)*(1-u)*x[2]
+pow(u,3)*x[3];
yu = pow(1-u,3)*y[0]+3*u*pow(1-u,2)*y[1]+3*pow(u,2)*(1-u)*y[2]
+pow(u,3)*y[3];
printf("X: %i Y: %i \n" , (int)xu , (int)yu) ;
}
}
int main(void) {
int x[] = {0,75,50,300};
int y[] = {0,2,140,100};
bezierCurve(x,y);
return 0;
}
https://ideone.com/glLXcB
Just a note: If you are using the usual formulas presented here then don't expect t = 0.5 to return the point at half of the curve's length.. In most cases it won't.
More on this here under "§23 — Tracing a curve at fixed distance intervals" and here.

Convert Screen Size (Pixels) to Latitude & longitude

Ok, so I am trying to write a code which returns back the values of all the 4 corner position of the map according to current view able area. For example, a user access Google maps through his device, he focuses on a particular area and hits a button and gets back the position (latitude and longitude) of all the four corners.
For this I have the center coordinate and now all I want span of the current viewable area and get the top left and bottom right corner coordinates. for this I used,
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x; // returns 480
int height = size.y; // returns 800
Now I want to convert them into latitude and longitude values so that I can use simple math such as:
topLeftLat= centerLat + lat/2;
topLeftLon= centerLon - lng/2;
bottomRightLat= centerLat - lat/2;
bottomRightLon = CenterLon + lng/2;
This would give me the corner cordinates.
lat and lng being the converted sceen resolution in latitude and longtiude values.
This took 5 minutes search on google so I don't know why I'm giving you this ... learning to research is the biggest part of being a developer.
GeoPoint mapCenter = myMap.getMapCenter();
float centerLatitude = (float)(mapCenter.getLatitudeE6())/1000000;
float centerLongitude = (float)(mapCenter.getLongitudeE6())/1000000;
float latitudeSpan = (float)(myMap.getLatitudeSpan())/1000000;
float longitudeSpan = (float)(myMap.getLongitudeSpan()/1000000);
GeoPoint mapTopLeft = myMap.getProjection().fromPixels(0, 0);
float topLatitude = (float)(mapTopLeft.getLatitudeE6())/1000000;
float leftLongitude = (float)(mapTopLeft.getLongitudeE6())/1000000;
GeoPoint mapBottomRight
= myMap.getProjection().fromPixels(myMap.getWidth(), myMap.getHeight());
float bottomLatitude = (float)(mapBottomRight.getLatitudeE6())/1000000;
float rightLongitude = (float)(mapBottomRight.getLongitudeE6())/1000000;
String info =
"Center: " + centerLatitude + "/" + centerLongitude + "\n"
+ "Top Left: " + topLatitude + "/" + leftLongitude + "\n"
+ "Bottom Right: " + bottomLatitude + "/" + rightLongitude + "\n"
+ "latitudeSpan: " + latitudeSpan + "\n"
+ "longitudeSpan: " + longitudeSpan + "\n";
Toast.makeText(AndroidMapActivity.this,
info,
Toast.LENGTH_LONG).show();
}};
The full tutorial is available at Android-er: Get the the coordinates (Latitude and Longitude) currently displayed on MapView

Is it possible to give a sexagesimal input for Latitude and Longitude

i am trying to figure out if you can give a sexagesimal input as a location , just like you can give a decimal input in android like this:
// Decimal input
double src_lat = 46.550952;
double src_long = 15.651366;
GeoPoint srcGeoPoint = new GeoPoint((int) (src_lat * 1E6),
(int) (src_long * 1E6));
so my question is as follows - is there a way to enter sexagesimal numbers and not decimal?
I have sexagesimal numbers in my database, in the form of "46.550952, 15.651366, etc."
but when i enter them as decimals it obviously gives me the wrong location on Google Maps , which is a problem.
I would be really gratefull for any answers or findings.
--- EDIT ---
I just gave up and made a function that turns sexagesimal into decimal =) Since all of the strings (numbers) i get are 10 chars long i can simply substring the minutes and seconds.
the code is below:
private double turnSexagesimalToDecimal(String number) {
double sumDivMinutes = 0;
String degree = number;
String degrees = degree.substring(0, 2);
String minutes = degree.substring(3, 5);
String second1 = degree.substring(5, 7);
String second2 = degree.substring(7);
String seconds = second1 + "." + second2;
double divideSeconds = Double.parseDouble(seconds) / 60;
sumDivMinutes = (Double.parseDouble(minutes) + divideSeconds) / 60;
sumDivMinutes = (double)Math.round(sumDivMinutes * 1000000) / 1000000;
sumDivMinutes = sumDivMinutes + Double.parseDouble(degrees);
return sumDivMinutes;
}
why not just write a function to convert between the two?
double decimalToSexagecimal(double decimalDegrees) {
String dd = new String(decimalDegrees);
String[] ddArr = dd.split(".");
// .75/100 = 75, .75 * 60/100
double sd = Double.valueOf(ddArr).doubleValue() * 60 / 100;
return Double.valueOf(ddArr[0] + "." + sd);
}
sexagecimalToDecimal(double sexagecimalDegreees) { ... }

Convert decimal feet to feet and inches?

How would I go about converting a measurement from, for example, 12.5 feet to 12ft 6in? How would I created that second number which reads only the decimal place when multiplying by 12?
right now I have double Measurement01 using other variables and some math to get me the feet in decimals. I send that to a textview with farf.setText(Measurement01 + " " + "ft");
Any help would be appreciated!
Substract the integer portion:
float measurement = 12.5;
int feet = (int)measurement;
float fraction = measurement - feet;
int inches = (int)(12.0 * fraction);
Quite simply, where length is the floating point length:
int feet = (int)length;
int inches = (length - feet) * 12.0;
: :
farf.setText (feet + " ft, " + inches + " inches");
Building on #Ry4an's answer:
//... Other code above
float Measurement01 = 12.5;
int feet = (int)Measurement01;
float fraction = Measurement01 - feet;
int inches = (int)(12.0 * fraction);
// Display like: 2.5 = 2 ft 6 in, 0.25 = 3 in, 6.0 = 6 ft
farf.setText((0 != feet ? feet + " ft" : "") + (0 != inches ? " " + inches + " in" : ""));

Categories

Resources