Is it possible to use color.scale to create color graded polygons in a polar.plot with plotrix? - r

I have been trying to map sound levels in multiple directions from a single sound source. I have average dB readings from 45 degree intervals around the source. I have plotted these using the polar.plot function in the plotrix package with my data represented as a polygon.
I would like to color the polygon so that higher values are more easily distinguished from lower ones using a color gradient (e.g. red for higher values, green for lower ones). I have attempted to do this using the color.scale function (also from plotrix).
>dB<-runif(9, min=17, max=24)
>azimuth<-seq(0,360,by=45)
>plot1<- polar.plot(dB,azimuth, main="Directional Signal Levels (dB)", start=90, clockwise=TRUE, rp.type="polygon", radial.lim=c(0,24), poly.col=color.scale(dB,c(0,1,1), c(1,1,0),0), boxed.radial=FALSE)
However, this seems to only generate a solid red polygon.
Is there a way to get the polygon to use the specified color gradient I have provided? Or is there another package that will allow me to specify the color gradient for a polygon if this one will not?

Related

Fit surface to 3 to 100 points where z values are 3D

I found myself in quite a big problem. I am average in math and I need to solve something, which is not very covered on the internet.
My problem: I have 2D space defined by X and Y. This space is just a drawing space. I want to assign to particular Xs,Ys a color with RGB values.
So let says I have 4 points with defined position in XY and color in Z:
[0,0, [255,0,0]]
[0,10, [0,255,0]]
[10,10,[0,0,255]]
[5,5, [0,0,0]]
and my drawing space is xy: 15x15.
And I want to distribute the colors to all empty points
For me its quite a delicate problem, because Z axis is basicly 3D space by itself.
My whole intention is to create a color map in which points 1,2,3,4 have between them smooth transition.
I am able to solve this in 1D where the transition is between 2 points. But I need to create 2D color map in XY drawing space based on fitted surface to these 4 points, which kind of depend both on the space of 3D-RGB and distance between them in XY drawing space.
Thanks in advance for help
You do not show any algorithm or code, so I will just explain a high-level algorithm. If you need more details or code or mathematical formulae, show more of your own work then ask. You do not explain just what you mean by "smooth transition"--there are multiple meanings. This will result in continuous shading but may not be smooth enough for your purposes.
First, given your points in the rectangular drawing space, find the Voronoi diagram for those points. This divides the drawing space into convex polygons, each polygon around one of your points.
For each vertex in the Voronoi diagram, figure which points are closest to the vertex--there will usually be just three of your points but there could be more. Then at that vertex point, assign the color that is the average of the RGB values of the nearby given points. That is, average the R values and the G values and the B values separately.
For any point on a Voronoi polygon edge, its color is the weighted average of the two colors at the endpoints. I.e. If the point is one-third of the distance from one end, its RGB value is one-third of the distance from the values at the endpoints.
Finally, for any point inside a Voronoi polygon, calculate the ray from the point that defined that polygon (the "center point") through the current point you are looking at. Find where that ray intersects the polygon. The RGB value is then the weighted average of the values of the center point and the polygon-intersection point.
The hardest part of all that is finding the Voronoi diagram. Fortune's algorithm can do this in a reasonable time. You can probably find a library to do that for you in your chosen programming language.
Another algorithm is to start with a triangulation of your given points and the corners of the drawing region. Then the color of any point in a triangle is the weighted average of the colors of the vertices. This will be automatically consistent for points on the vertices or edges of the triangles, so this is probably simpler than my previous algorithm. The difficulty here is finding a triangulation (any will do).

Domain coloring (color wheel) plots of complex functions in Octave (Matlab)

I understand that domain or color wheel plotting is typical for complex functions.
Incredibly, I can't find a million + returns on a web search to easily allow me to reproduce some piece of art as this one in Wikipedia:
There is this online resource that reproduces plots with zeros in black - not bad at all... However, I'd like to ask for some simple annotated code in Octave to produce color plots of functions of complex numbers.
Here is an example:
I see here code to plot a complex function. However, it uses a different technique with the height representing the Re part of the image of the function, and the color representing the imaginary part:
Peter Kovesi has some fantastic color maps. He provides a MATLAB function, called colorcet, that we can use here to get the cyclic color map we need to represent the phase. Download this function before running the code below.
Let's start with creating a complex-valued test function f, where the magnitude increases from the center, and the phase is equal to the angle around the center. Much like the example you show:
% A test function
[xx,yy] = meshgrid(-128:128,-128:128);
z = xx + yy*1i;
f = z;
Next, we'll get its phase, convert it into an index into the colorcet C2 color map (which is cyclic), and finally reshape that back into the original function's shape. out here has 3 dimensions, the first two are the original dimensions, and the last one is RGB. imshow shows such a 3D matrix as a color image.
% Create a color image according to phase
cm = colorcet('C2');
phase = floor((angle(f) + pi) * ((size(cm,1)-1e-6) / (2*pi))) + 1;
out = cm(phase,:);
out = reshape(out,[size(f),3]);
The last part is to modulate the intensity of these colors using the magnitude of f. To make the discontinuities at powers of two, we take the base 2 logarithm, apply the modulo operation, and compute the power of two again. A simple multiplication with out decreases the intensity of the color where necessary:
% Compute the intensity, with discontinuities for |f|=2^n
magnitude = 0.5 * 2.^mod(log2(abs(f)),1);
out = out .* magnitude;
That last multiplication works in Octave and in the later versions of MATLAB. For older versions of MATLAB you need to use bsxfun instead:
out = bsxfun(#times,out,magnitude);
Finally, display using imshow:
% Display
imshow(out)
Note that the colors here are more muted than in your example. The colorcet color maps are perceptually uniform. That means that the same change in angle leads to the same perceptual change in color. In the example you posted, for example yellow is a very narrow, bright band. Such a band leads to false highlighting of certain features in the function, which might not be relevant at all. Perceptually uniform color maps are very important for proper interpretation of the data. Note also that this particular color map has easily-named colors (purple, blue, green, yellow) in the four cardinal directions. A purely real value is green (positive) or purple (negative), and a purely imaginary value is blue (positive) or yellow (negative).
There is also a great online tool made by Juan Carlos Ponce Campuzano for color wheel plotting.
In my experience it is much easier to use than the Octave solution. The downside is that you cannot use perceptually uniform coloring.

Masking a low quality raster with polygons, getting weird overhang

I am pretty new to R, and have been attempting to use the mask function on a raster image of 250mx250m resolution. My problem is that for some reason I am getting overhang, as there are pixels which lie both inside and outside of the polygon. Is there a way to tighten the tolerance level of mask so that only the pixels within a certain percentage inside the polygon are accepted?
green is my polygon, blue is the resulting mask
I am guessing that you are using the rasterize function from the raster package.
The grid cells are rather large relative to the polygons you are using. rasterize uses the center of the cell to determine if it is covered. However, if you use argument getCover=TRUE you will get a value between 1 to 100 indicating the percentage of each cell that is covered. You could then use a threshold of your choice.
Source: Masking low quality raster with polygons in R gives weird overhang?

Compute a radius scale factor to construct n-sided reqular convex polygons of equal area

I have a computer graphics plotting application where we often plot regular convex polygon shapes as symbols for different data points. I'd like to scale the radius (aka circumradius, distance from center to vertex) of the polygons so that polygons with different numbers of sides all have equal area (so presumably similar perceptual impact). i.e. if a circle with radius=1 has area Pi*radius^2, how much do I need to scale the radius to get a square or a triangle with the same area? What would the formula be to compute this for regular polygons with arbitrary numbers of sides?
Seems like this should be a simple geometry/algebra problem, but that was a long time ago... :-)
Using the formula below (taken from this site):
one can derive that:
R = sqrt(2*area / (N*sin(2*pi/N)))

How to smoothly interpolate color along a Java2D path?

I'm from a 3D rendering background where this is trivial to do, but I can't find how to do this in Java2D:
Say I have points A, B and colors cA, cB.
The points are joined together by a quadTo() command and stroked. I want the colors to smoothly interpolate between each point. i.e. point A is color cA, point B is color cB, and the color of the line between A and B linearly interpolates between cA and cB.
I've tried stroking with a gradient fill but for my purposes it is far too slow, and also produces inaccurate results since I am actually joining up hundreds of these curve segments into continuous paths (using GeneralPath).
Is good 'ole point to point linear color interpolation not supported in Java2D?
The answer is: you can't do it. Java2D doesn't support this operation.

Resources