Convert decimal feet to feet and inches? - android

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" : ""));

Related

Converting height in centimeters to inches but cant get decimal points

Trying to convert centimeters to inches then round to the nearest half inch and print 1 decimal point.
3.1 = 3.0
3.2 = 3.0
3.3 = 3.5
3.6 = 3.5
3.8 = 4.0
float index;
float height;
index = (Math.round((height * .393701)*2))/2;
text.setText("Index: " + index);
When I print index it wont show the decimal. Once the number reaches .75 or higher it rounds to the next higher whole number.
Try this,
float index;
float height;
index = (float) (Math.round((height / 2.54) * 10) / 10.0);
text.setText("Index: " + index);
I put an f at the end of each number.
(Math.round((height / 2.54f)*2f)/2f)
Height
147 / 2.54 = 57.874016 = 58
148 / 2.54 = 58.26772 = 58.5

Error in converting height from centimeters to feet inches

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);
}

Setting locations latitude & longitude in facing direction

How do you clone your own Location, and then add some extra metres towards a desired direction?
Right now my android app can get the azimut, and my GPS location, and I hardcoded the desired distanct for an example, 10 metres.
Then my app should create a new Location, exactly 10 metres ahead where im facing.
So if my position is: longitude 17.537200603552526, latitude 25.70358496181195
and i want to it to move 8 metres directly towards north, and 2 metres directly towards east.
Would the new longitude and latitude look like:
current longitude + 0.00008 , current latitude + 0.00002
Heres some of my code to calculate the gradient and the new location:
public double[] calculateXYLength(double[] vals, double distance)
{
double x = vals[0];
double y = vals[1];
if (vals[0] < 0)
{
x = x * -1;
}
if (vals[1] < 0)
{
y = y * -1;
}
// Log.d("lengths", "K = " + distance + " / (" + x + " + " + y + " )" );
double k = distance / (x + y);
// Log.d("lengths", "vals[0]: " + vals[0] + " vals[1]: " + vals[1]);
// Log.d("lengths", "x: " + (vals[0] * k) + " y: " + (vals[1] * k) +
// " k: " + k);
return new double[] { (vals[0] * k), (vals[1] * k) };
}
Notice the vals is the gradient i already calculated, and the result i'm returning i'm going to use to add to my new location point.
Location newPosition = currentPosition
newPosition.setLongitude(myPosition.getLongitude() + val[0]);
newPosition.setLatitude(myPosition.getLatitude() + val[1]);
But the thing is, I don't get the desired distance between these two locations.. And I can't figure out why. How do I add the desired distance on the latitude/longitude?
Thanks,
Johan
The measures of longitude and latitude are degrees not meters, I think that is the problem.
1 degree = 111.12 km

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

Categories

Resources