Convert a decimal value in system.drawing.point - visual-studio-2010

I have two variables declared as decimal
Dim datok As Decimal
Dim datol As Decimal
both were filled with values from rs232 port and Ineed to draw a line with these points, but when I try to pass the values of datok and datol to a point structure the error "Value of type 'Decimal' cannot be converted to 'system.drawing.point'
How I can convert the value decimal of datok in a system.drawing.point
Thanks in advance

You can use the PointF Structure.
From above link:
Represents an ordered pair of floating-point x- and y-coordinates that defines a point in a two-dimensional plane.
i.e.
Dim datok As Decimal
Dim datol As Decimal
Dim myPoint As PointF = New PointF(datok, datol)
I am not sure how you are drawing your lines, but there are corresponding Graphics Methods the use the PointF Structure instead of Point as in this DrawLine Method
Or if you are content to have some data loss you can just convert the decimals to integers when you create your Point
i.e.
Dim myPoint As Point = New Point(CInt(datok), CInt(datol))

Related

How to multiply two int variables in Ruby

I am making a program to enter the radius and output the area and the perimeter:
# Calculates the area and the perimeter of a circle given the radius
puts "Enter a radius"
radius = gets
area = radius * 3.14 * radius
perimeter = 2 * 3.14 * radius
puts area
puts perimeter
When I tried to execute the code, it returned:
area.rb:4:in `*': no implicit conversion of String into Integer (TypeError)
The compiler says that the error is on the fourth line, but far I don't see any problems.
The problem is that radius is a string, because gets always returns a string. Maybe it contains an integer, but it's a string representation of an integer, like '10.3'.
radius = gets.to_i
or if you need decimal values
radius = gets.to_f
It's a bit more complex than this because to_i and to_f give you 0 and 0.0 respectively if you called them on 'foo'. In that case you could use the Integer and Float methods, and they will give you an ArgumentError exception if they are not able to convert to an integer or float value.

Matlab conversion of pixel values to double precision

I'm new to Matlab so this should be an easy question. I have an image with a few specific pixels that I need to get the red RGB components from, sum them, and store the result into a variable. By default, these values are of type uint8, so the sum can't exceed 255. I've tried using every combination of double() to convert the R value to a double, but nothing seems to be working. Here's exactly what's happening, copied from the terminal: (All pixels have R values above 200)
img = imread('img.png');
r = img(64,64,1)
r =
224
r = r + double(img(64,65,1))
r =
255
r = r + double(img(64,66,1))
r =
255
What am I doing wrong? Am I not able to convert these values to double?
For image processing, most of the times it is a good idea to use the im2double function to convert the image read into a double array between 0-1 as such:
img = im2double(imread('img.png'));
Then you don't need to worry about data type any more in the same program.
The data type conversion with double() alone almost never do what you intended to images, since uint8 and double images differ in both data type and data range.
What's happening in the line r = r + double(img(64,65,1)) is that the img(64,65,1) value is being converted to double, but then immediately being converted back into class(r) because r is an integer class and has precedence in operations. Observe that class(int64(10)+10) gives back int64. The key here is, as beaker commented, to convert r itself to double first.
You may not have to worry about using double() to do the conversion; doubles can represent integers up to 2^53-1, which is way higher than 255. So if you're just doing simple pixel-summing operations or things like that, you're not going to lose any precision in the calculations. Of course, if you need to put it back into an image, then anything about 255 will saturate. So, it might make more sense depending on what you're doing to rescale things to be between 0 and 1; in that case, as Mingjing suggested, it's best to use im2double.

Definite steps with variable range on graph axis in Stata

I am producing a number of charts in a loop, using example code below
forvalues i = 1/11 {
local t: label (countryID) `i'
twoway line Var1 Var2 Var3 year if countryID == `i', ylabel(, valuelabel grid ///
angle(horizontal)) xlabel(1999(2)2013) xtitle("") ///
lpattern("l" "dash_dot" "longdash") legend(size(small)) title("`t'")
graph export output/PredictLaw`t'.png, replace
}
The values of each variable are labeled, but only at integer level. The variable itself can take any value between two integers, though. Consequently, I would like to ensure somehow that the yaxis uses only integers as distance, e.g. always in steps of 1.
However, since the range for each country is different, I would like Stata to determine how long the range of the y-axis has to be, as shorter ranges of course produce neater looking graphs. Hence, I would rather not use something in the form of ylabel(1(1)21, ....
Is there a way to define the spacing of the y-axis, while keeping the range of the y-axis a Stata determined variable?
After
summarize whatever, meanonly
local ymin = floor(r(min))
local ymax = ceil(r(max))
give integer results you can invoke in graph calls. The small detail here that you round down the minimum, if you do any rounding, and round up the maximum, ditto, given floor() and ceil() functions, could add a smidgen of space that suits graphical purposes.

create 3D image from coordinates and intensity values

I am trying to create a 3D array of size 1000x1000x1000 with all the elements (corresponding to voxels) being zero and then assign a random value in the 2000 to 2001 range instead of 0 to some specific elements in the array and finally store it as a binary file.
The array named "coord" is the Nx3 matrix coordinates (x,y,z) of the points that I need them to be assigned the random value in the 3D array.))
I should mention that all the x,y,z values of the coordinate matrix are floating point numbers with: 0<=x<=1000 0<=y<=1000 0<=z<=1000
My aim is to export the 3D matrix in a binary format (other than MATLAB's default binary format) so that I can use it with other programs.
Here is what I've been up to so far:
load coord;
a=coord(:,1);
b=coord(:,2);
c=coord(:,3);
d=rand(1000,1)*2000;
dd = 0:2:1000;
[xq,yq,zq] = meshgrid(dd,dd,dd);
vq = griddata3(a,b,c,d,xq,yq,zq,'nearest');
h=figure;
plot3(a,b,c,'ro')
%=========================================%
fid=fopen('data.bin','w');
fwrite(fid,vq,'single');
fclose(fid);
In the above code a, b and c are the coordinates of each point and d is the corresponding intensity values for the desired range. While it is possible to create a 3D mesh (using meshgrid) and then interpolate the intensity values for mesh points (using griddata3), the final result (vq) would not be the actual points (ai,bi,ci) and corresponding intensities , but rather an interpolated set of points which is pretty useful for visualization purposes (for instance if you like to fit a 3D surface which fits through actual data).
I am simply trying to find a way to store the actual data-points and their intensities into a file and export it.
Any help is highly appreciated.
If you want to save to files that will allow importing into a visualization software, a series of Tiff files will most likely be convenient, i.e.
maxValue = 2000; % this is the maximum signal that can possibly occur
% according to your code
for z = 1:size(vq,3)
%# convert slice z to 16 bit
currentSlice = vq(:,:,z);
currentSlice = uint16(round(currentSlice/maxValue))
%# save to file
imwrite(currentSlice, sprintf('testImg_z%04i.tif',z),'tif');
end
Note that if you create a double array of dimensions 1000x1000x1000, you'll need 8GB of contiguous RAM.
How about something like:
%# 3D array
voxels = zeros([1000 1000 1000]);
%# round points coordinates, and clamp to valid range [1,1000]
load coords
coords = round(coords);
coords = min(max(coords,1),1000);
%# convert to linear indices
idx = sub2ind(size(voxels), coords(:,1), coords(:,2), coords(:,3));
%# random values in the 2000 to 2001 range
v = rand(size(idx)) + 2000;
%# assign those values to the chosen points
voxels(idx) = v;

Displaying a subset of longitude/latitude points?

I have an array of coordinates (latitude and longitude) maded in this way:
[0] = "45.01234,9.12345"
[1] = "46.11111,9.12345"
[2] = "47.22222,9.98765"
[...] etc
In a loop, convert these coordinates in meters (UTM northing / UTM easting) and after that I convert these coords in pixel (X / Y) on screen (the output device is an iPhone) to draw a route line on a custom map.
[0] = "512335.00000,502333.666666"
[...] etc
The returning pixel are passed to a method that draw a line on screen (simulating a route calculation).
[0] = "20,30"
[1] = "21,31"
[2] = "25,40"
[...] etc
As coordinate (lat/lon) are too many, I need to truncate lat/lon array eliminating the values that doesn't fill in the map bound (the visible part of map on screen).
Map bounds are 2 couple of coords lat/lon, upper left, and lower right.
Now, what is the best way to loop on this array (NOT SORTED) and check if a value is or not in bound and after remove the value that is outside?
To return a clean array that contains only the coords visible on screen?
Note: the coords array is a very big array. 4000/5000 Couple of items.
This is a method that should be looped every drag or zoom.
How can I optimize search and controls in this array?
I'd suggest breaking this into several steps:
Convert each longitude/latitude pair into a pair of meters in your new coordinate system.
Create a kd-tree data structure holding all the points in the set. This allows you to efficiently query which points lie in a given rectangular range very efficiently.
Whenever the viewport changes, find all points in the kd-tree that will be displayed in that rectangle.
To display the points, iterate over the set of points that will be displayed and display each of them.

Resources