gradient bars Chart JS - angularjs

I want to be able to color each bar with a different color in a gradient way like the folloing image
Can any body tell me how to do that?

This is using ChartJS 2.4.0. http://www.chartjs.org/docs/latest/charts/bar.html
Given x number of datapoints in your Bar Graph, you'll need to generate an array of x number of colors: one color per each datapoint.
data: [10,20,30,40,50,60,70,80],
backgroundColor: ["#ded21e", "#cbd026", "#b8ce2d", "#a5cc35", "#93cb3c", "#80c944", "#6dc74b", "#5ac553"]
Here is a CodePen: https://codepen.io/pgardner/pen/GyeBLW
Credit goes to Neil McCallion, whose CodePen here does much of the legwork: https://codepen.io/njmcode/pen/axoyD
Here is the essential function:
function color() {
// Converts a #ffffff hex string into an [r,g,b] array
function hex2rgb(hex) {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? [
parseInt(result[1], 16),
parseInt(result[2], 16),
parseInt(result[3], 16)
] : null;
}
// Inverse of the above
function rgb2hex(rgb) {
return '#' + ((1 << 24) + (rgb[0] << 16) + (rgb[1] << 8) + rgb[2]).toString(16).slice(1);
}
// Interpolates two [r,g,b] colors and returns an [r,g,b] of the result
// Taken from the awesome ROT.js roguelike dev library at
// https://github.com/ondras/rot.js
function _interpolateRgb(color1, color2, factor) {
if (arguments.length < 3) { factor = 0.5; }
let result = color1.slice();
for (let i=0;i<3;i++) {
result[i] = Math.round(result[i] + factor*(color2[i]-color1[i]));
}
return result;
}
function rgb2hsl(color) {
const r = color[0]/255;
const g = color[1]/255;
const b = color[2]/255;
const max = Math.max(r, g, b), min = Math.min(r, g, b);
let h, s, l = (max + min) / 2;
if (max == min) {
h = s = 0; // achromatic
} else {
let d = max - min;
s = (l > 0.5 ? d / (2 - max - min) : d / (max + min));
switch(max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
return [h, s, l];
}
function hue2rgb(p, q, t) {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1/6) return p + (q - p) * 6 * t;
if (t < 1/2) return q;
if (t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
}
function hsl2rgb(color) {
let l = color[2];
if (color[1] == 0) {
l = Math.round(l*255);
return [l, l, l];
} else {
let s = color[1];
let q = (l < 0.5 ? l * (1 + s) : l + s - l * s);
let p = 2 * l - q;
let r = hue2rgb(p, q, color[0] + 1/3);
let g = hue2rgb(p, q, color[0]);
let b = hue2rgb(p, q, color[0] - 1/3);
return [Math.round(r*255), Math.round(g*255), Math.round(b*255)];
}
}
function _interpolateHsl(color1, color2, factor) {
if (arguments.length < 3) { factor = 0.5; }
let hsl1 = rgb2hsl(color1);
let hsl2 = rgb2hsl(color2);
for (let i=0;i<3;i++) {
hsl1[i] += factor*(hsl2[i]-hsl1[i]);
}
return hsl2rgb(hsl1);
}
function generateGradient(color1, color2, total, interpolation) {
const colorStart = typeof color1 === 'string' ? hex2rgb(color1) : color1;
const colorEnd = typeof color2 === 'string' ? hex2rgb(color2) : color2;
// will the gradient be via RGB or HSL
switch(interpolation) {
case 'rgb':
return colorsToGradientRgb(colorStart, colorEnd, total);
case 'hsl':
return colorsToGradientHsl(colorStart, colorEnd, total);
default:
return false;
}
}
function colorsToGradientRgb(startColor, endColor, steps) {
// returns array of hex values for color, since rgb would be an array of arrays and not strings, easier to handle hex strings
let arrReturnColors = [];
let interimColorRGB;
let interimColorHex;
const totalColors = steps;
const factorStep = 1 / (totalColors - 1);
for (let idx = 0; idx < totalColors; idx++) {
interimColorRGB = _interpolateRgb(startColor, endColor, factorStep * idx);
interimColorHex = rgb2hex(interimColorRGB);
arrReturnColors.push(interimColorHex);
}
return arrReturnColors;
}
function colorsToGradientHsl(startColor, endColor, steps) {
// returns array of hex values for color, since rgb would be an array of arrays and not strings, easier to handle hex strings
let arrReturnColors = [];
let interimColorRGB;
let interimColorHex;
const totalColors = steps;
const factorStep = 1 / (totalColors - 1);
for (let idx = 0; idx < totalColors; idx++) {
interimColorRGB = _interpolateHsl(startColor, endColor, factorStep * idx);
interimColorHex = rgb2hex(interimColorRGB);
arrReturnColors.push(interimColorHex);
}
return arrReturnColors;
}
return {
generateGradient
};
}
Given 2 colors '#ded21e' and '#5ac553', you would call the function in this manner:
let arrBarGraphColors = color().generateGradient('#ded21e', '#5ac553', data.length, 'rgb');
Specify between 'rgb' and 'hsl'. The linked CodePens should give you an idea of the differences between the two.

Related

function in queen's Attack II returns wrong answer

I'm solving Queen's Attack II problem in Hackerrank. My idea is traverse through every obstacles to find the closest obstacle in each directions (left, right, up down and 4 diagons. My function only passed half of testcases. Then I search on the Internet and I found a similar solution but different in code structure, obsiously it passed all testcases.
My function:
int queensAttack(int n, int k, int r_q, int c_q, int obstacles_rows, int obstacles_columns, int **obstacles) {
long a[8];
a[0]=c_q-1;
a[1]=n-c_q;
a[2]=r_q-1;
a[3]=n-r_q;
a[4]=min(a[3], a[1]);
a[5]=min(a[2], a[1]);
a[6]=min(a[2], a[0]);
a[7]=min(a[3], a[0]);
int x=obstacles[i][1], y=obstacles[i][0];
for (int i=0; i<obstacles_rows; i++) {
if(y==r_q && x<c_q) a[0]=min(a[0], c_q-x-1);
else if(y==r_q && x>c_q) a[1]=min(a[1], x-c_q-1);
else if(x==c_q && y<r_q) a[2]=min(a[2], r_q-y-1);
else if(x==c_q && y>r_q) a[3]=min(a[3], y-r_q-1);
else if(y-r_q==x-c_q) a[4]=min(a[4], y-r_q-1);
else if(r_q-y==x-c_q) a[5]=min(a[5], r_q-y-1);
else if(r_q-y==c_q-x) a[6]=min(a[6], r_q-y-1);
else if(y-r_q==c_q-x) a[7]=min(a[7], y-r_q-1);
}
int result=0;
for (int i=0; i<8; i++) {
result+=a[i];
}
return result;
}
Their fucntion:
int queensAttack(int n, int k, int r_q, int c_q, int obstacles_rows, int obstacles_columns, int **obstacles) {
a[0] = c_q -1 ;
a[1] = n -r_q;
a[2] = n -c_q;
a[3] = r_q -1;
a[4] = min(a[0],a[1]);
a[5] = min(a[1],a[2]);
a[6] = min(a[2],a[3]);
a[7] = min(a[3],a[0]);
int x,y;
while (--k >= 0)
{
y = obstacles[k][0] - r_q;
x = obstacles[k][1] - c_q;
if (x == 0)
{
if (y > 0)
{
a[1] = min(a[1], y - 1);
}
else
{
a[3] = min(a[3], -(y + 1));
}
}
else if (y == 0)
{
if (x > 0)
{
a[2] = min(a[2], x - 1);
}
else
{
a[0] = min(a[0], -(x + 1));
}
}
else
{
float m = (float)y / x;
if (m == 1.0)
{
if (x > 0)
{
a[5] = min(a[5], x - 1);
}
else
{
a[7] = min(a[7], -y - 1);
}
}
else if (m == -1.0)
{
if (x > 0)
{
a[6] = min(a[6], x - 1);
}
else
{
a[4] = min(a[4], y - 1);
}
}
}
}
int result=0;
for (int i=0; i<8; i++) {
result+=a[i];
}
return result;
}
Where am I wrong?
Any help would be appreciated.
Some issues:
1) This line should not occur before the loop, but as a first statement inside the loop body:
int x=obstacles[i][1], y=obstacles[i][0];
2) The second half of the loop has conditions which cause a problem. For instance when y-r_q==x-c_q is true, then also r_q-y==c_q-x is true, and vice versa. So this expression tells you whether an obstacle is on a queen's diagonal, but not on which side of the queen. As a consequence, the argument you pass to the min function could be negative, and this should never happen. You need an additional condition, much like you have in the first half of that if-chain. So change the second half to this:
else if(y-r_q==x-c_q && y>r_q) a[4]=min(a[4], y-r_q-1);
else if(r_q-y==x-c_q && y<r_q) a[5]=min(a[5], r_q-y-1);
else if(r_q-y==c_q-x && y<r_q) a[6]=min(a[6], r_q-y-1);
else if(y-r_q==c_q-x && y>r_q) a[7]=min(a[7], y-r_q-1);
It took me some time to figure out this one. My first code couldn't get through the time execution time limits. So, some math comes handy. First I calculate the total number of moves as there are no obstacles, just using math. If there are no obstacles that is the answer.
If there are obstacles, loop through them, using boolean to close each direction that is found and using math calculate the non moves. But I still had error, because the obstacles had to be sorted from the queen to the farthest obstacles position.
Here is my code in Java.
int vertHor = n - 1;
int d1 = Math.min(n - c_q, n - r_q) + Math.min(r_q - 1, c_q - 1);
int d2 = Math.min(c_q - 1, n - r_q) + Math.min(n - c_q, r_q - 1);
int total = vertHor * 2 + d1 + d2;
if (k == 0) {
return total;
}
boolean u = false;
boolean d = false;
boolean l = false;
boolean r = false;
boolean ul = false;
boolean ur = false;
boolean dl = false;
boolean dr = false;
Arrays.sort(obstacles, (int[] x, int[] y) -> Math.min(Math.abs(x[0] - r_q), Math.abs(x[1] - c_q))
- Math.min(Math.abs(y[0] - r_q), Math.abs(y[1] - c_q)));
for (int[] obs : obstacles) {
int oRow = obs[0];
int oCol = obs[1];
if (oRow == r_q) {
if (oCol < c_q && !l) {
total -= oCol;
l = true;
} else if (!r) {
total -= n - oCol + 1;
r = true;
}
} else if (oCol == c_q) {
if (oRow < r_q && !d) {
total -= oRow;
d = true;
} else if (!u) {
total -= n - oRow + 1;
u = true;
}
} else if (Math.abs(c_q - oCol) == Math.abs(r_q - oRow)) {//oCol != c_q && oRow != r_q) {
if (oCol < c_q && oRow > r_q && !ul) {
total -= Math.min(n - oRow + 1, oCol);
ul = true;
} else if (oCol < c_q && oRow < r_q && !dl) {
total -= Math.min(oRow, oCol);
dl = true;
} else if (oCol > c_q && oRow < r_q && !dr) {
total -= Math.min(oRow, n - oCol + 1);
dr = true;
} else if (oCol > c_q && oRow > r_q && !ur) {
total -= Math.min(n - oRow, n - oCol) + 1;
ur = true;
}
}
if (u && d && l && r && ul && ur && dl && dr) {
break;
}
}
return total;

Highcharts logarithmic scale with very small numbers

I'm using this function to display negative values on logarithmic scale. Everything works fine except if I put very small numbers like :
[8, 6.986e-8],[14.947368421052632, 1.705e-7],[21.894736842105264, 3.299e-7],[28.842105263157897, 5.606e-7],[35.78947368421053, 8.765e-7],[42.73684210526316, 0.000001292]
In this case y-axis won't be transfer to logaritmic. I don't have deep understanding of prototyping inner functions of highcharts. Highcharts version is 6.1.2.
// Pass error messages
H.Axis.prototype.allowNegativeLog = true;
// Override conversions
H.Axis.prototype.log2lin = function (num) {
var isNegative = num < 0,
adjustedNum = Math.abs(num),
result;
if (adjustedNum < 10) {
adjustedNum += (10 - adjustedNum) / 10;
}
result = Math.log(adjustedNum) / Math.LN10;
return isNegative ? -result : result;
};
H.Axis.prototype.lin2log = function (num) {
var isNegative = num < 0,
absNum = Math.abs(num),
result = Math.pow(10, absNum);
if (result < 10) {
result = (10 * (result - 1)) / (10 - 1);
}
return isNegative ? -result : result;
};
http://jsfiddle.net/9oLpayw1/ (need to put data)

How do I invert a matrix in tensorflow-js?

Simple question. Is there an operation for it, also, how do you create an identity matrix? I've checked the documentation but can't find a single operation.
For now tensorflowJs does not have any direct operator that can return the inverse of a matrix M.
You can consider using the module Math.js
Having said that, it is possible to invert a matrix using tensorflowJs operators.
The following calculates the inverse of a matrix using the adjoint method with tensorflowJs operators.
// calculate the determinant of a matrix m
function det(m) {
return tf.tidy(() => {
const [r, _] = m.shape
if (r === 2) {
const t = m.as1D()
const a = t.slice([0], [1]).dataSync()[0]
const b = t.slice([1], [1]).dataSync()[0]
const c = t.slice([2], [1]).dataSync()[0]
const d = t.slice([3], [1]).dataSync()[0]
result = a * d - b * c
return result
} else {
let s = 0;
rows = [...Array(r).keys()]
for (let i = 0; i < r; i++) {
sub_m = m.gather(tf.tensor1d(rows.filter(e => e !== i), 'int32'))
sli = sub_m.slice([0, 1], [r - 1, r - 1])
s += Math.pow(-1, i) * det(sli)
}
return s
}
})
}
// the inverse of the matrix : matrix adjoint method
function invertMatrix(m) {
return tf.tidy(() => {
const d = det(m)
if (d === 0) {
return
}
[r, _] = m.shape
rows = [...Array(r).keys()]
dets = [];
for (let i = 0; i < r; i++) {
for (let j = 0; j < r; j++) {
const sub_m = m.gather(tf.tensor1d(rows.filter(e => e !== i), 'int32'))
let sli
if (j === 0) {
sli = sub_m.slice([0, 1], [r - 1, r - 1])
} else if (j === r - 1) {
sli = sub_m.slice([0, 0], [r - 1, r - 1])
} else {
const [a, b, c] = tf.split(sub_m, [j, 1, r - (j + 1)], 1)
sli = tf.concat([a, c], 1)
}
dets.push(Math.pow(-1, (i + j)) * det(sli))
}
}
com = tf.tensor2d(dets, [r, r])
tr_com = com.transpose()
inv_m = tr_com.div(tf.scalar(d))
return inv_m
})
}
const a = tf.tensor2d([1, 3, 3, 1, 4, 3, 1, 3, 4], [3, 3])
console.log("matrix a")
a.print()
const i = invertMatrix(a)
console.log("inverse i")
i.print()
const prod = i.dot(a)
console.log("a * i")
prod.print()
<html>
<head>
<!-- Load TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/#tensorflow/tfjs#0.12.0"> </script>
</head>
<body>
</body>
</html>
Using the Jordan-Gauss method, one can calculate the inverse of the matrix this way:
// calculate the determinant of a matrix m
function det(m) {
return tf.tidy(() => {
const [r, _] = m.shape
if (r === 2) {
const t = m.as1D()
const a = t.slice([0], [1]).dataSync()[0]
const b = t.slice([1], [1]).dataSync()[0]
const c = t.slice([2], [1]).dataSync()[0]
const d = t.slice([3], [1]).dataSync()[0]
result = a * d - b * c
return result
} else {
let s = 0;
rows = [...Array(r).keys()]
for (let i = 0; i < r; i++) {
sub_m = m.gather(tf.tensor1d(rows.filter(e => e !== i), 'int32'))
sli = sub_m.slice([0, 1], [r - 1, r - 1])
s += Math.pow(-1, i) * det(sli)
}
return s
}
})
}
// the inverse of the matrix : jordan-gauss method
function invertM(m) {
return tf.tidy(() => {
if (det(m) === 0) {
return
}
const [r, _] = m.shape
let inv = m.concat(tf.eye(r), 1)
rows = [...Array(r).keys()]
for (let i = 0; i < r; i++) {
inv = tf.tidy(() => {
for (let j = i + 1; j < r; j++) {
const elt = inv.slice([j, i], [1, 1]).as1D().asScalar()
const pivot = inv.slice([i, i], [1, 1]).as1D().asScalar()
let newrow
if (elt.dataSync()[0] !== 0) {
newrow = inv.gather(tf.tensor1d([i], 'int32')).sub(inv.gather(tf.tensor1d([j], 'int32')).div(elt).mul(pivot)).as1D()
const sli = inv.gather(rows.filter(e => e !== j))
const arr = []
if (j === 0) {
arr.push(newrow)
}
sli.unstack().forEach((t, ind) => {
if (ind !== j) {
arr.push(t)
} else {
arr.push(newrow)
arr.push(t)
}
})
if (j === r - 1) {
arr.push(newrow)
}
inv = tf.stack(arr)
}
}
return inv
})
}
const trian = tf.unstack(inv)
len = trian.length
trian[len - 1] = trian[len - 1].div(trian[len - 1].slice(trian[len - 1].shape[0] - 1, 1).asScalar())
for (let i = r - 2; i > -1; i--) {
for (j = r - 1; j > i; j--) {
trian[i] = trian[i].sub(trian[j].mul(trian[i].slice(j, 1).asScalar()))
}
trian[i] = trian[i].div(trian[i].slice(i, 1).asScalar())
}
return tf.split(tf.stack(trian), 2, 1)[1]
})
}
const a = tf.tensor2d([1, 3, 3, 1, 4, 3, 1, 3, 4], [3, 3])
console.log("matrix a")
a.print()
const i = invertM(a)
console.log("inverse i")
i.print()
const prod = i.dot(a)
console.log("a * i")
prod.print()
<html>
<head>
<!-- Load TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/#tensorflow/tfjs#0.12.0"> </script>
</head>
<body>
</body>
</html>
For the identity matrix you can use tf.eye() and I am not sure what you mean by inverting a matrix but it could either be tf.reciprocal() which performs 1/x element-wise or you want to transpose it, which would be done using tf.transpose().

C language bresenhamLine Algorithm

I am confused about what could be the problem in this algorithm:
With these parameters:
ine temp = {(Vector){10, 150}, (Vector){120, 40}, polygon->color};
drawaLine(monitor, &temp);
However, when I change the coordinates to 120, 20 it fails:
Line temp = {(Vector){10, 150}, (Vector){120, 20}, polygon->color};
drawaLine(monitor, &temp);
My code is:
int changed = 0;
int x = line->start.x;
int y = line->start.y;
int dx = (line->end.x - line->start.x);
int dy = (line->end.y - line->start.y);
dx = (dx < 0) ? -dx : dx;
dy = (dy < 0) ? -dy : dy;
int signx;
int signy;
if ((line->end.x - line->start.x) == 0) {
signx = 0;
} else {
int tempsignx = (line->end.x - line->start.x);
signx = (tempsignx < 0) ? -1 : 1;
}
if ((line->end.y - line->start.y) == 0) {
signy = 0;
} else {
int tempsingy = (line->end.y - line->start.y);
signy = (tempsingy < 0) ? -1 : 1;
}
//swap values
if (dy > dx) {
int temp = dy;
dy = dx;
dx = temp;
changed = 1; //changed = true
}
int e = 2 * dy - dx;
for (int i = 1; i <= dx; i++) {
Vector temp = {x, y};
drawPixel(monitor, temp, line->color);
while (e >= 0) {
if (changed) {
x = x + signx;
} else {
y = y + signy;
e = e - 2 * dx;
}
}
if (changed) {
y += signy;
} else {
x += signx;
e = e + 2 * dy;
}
Vector x2y2 = {line->end.x, line->end.y};
drawPixel(monitor, x2y2, line->color);
}
}

Rotate a 2D NxN matrix in Concentric Circles

Given a 2D NxN matrix, visualize it as concentric circles. You have to find the rotated matrix where each element in the circle is rotated by 1 position layer by layer in an alternate clockwise and anticlockwise direction. All rotations should be in-place.
2 3 4 5
1 6 7 8
4 2 1 9
5 3 2 4
should get transformed to
1 2 3 4
4 7 1 5
5 6 2 8
3 2 4 9
I thought about the solution
1> For clockwise circle rotation, read elements in the order
i -> 0 to n-1 and j = 0
j -> 0 to n-1 and i = n-1
i -> n-1 to 0 and j = n-1
j -> n-1 to 0 and i = 0
2> For anti-clockwise circle rotation, read elements in the order
j -> 0 to n-1 and i = 0
i -> 0 to n-1 and j = n-1
j -> n-1 to 0 and i = n-1
i -> n-1 to 0 and j = 0
Code
for(int cnt = 0; cnt < n/2; cnt++)
{
if(cnt%2 == 0) // Clockwise
{
i = cnt; j = cnt;
// while loops for each case
}
else // anti-clockwise
{
i = cnt; j = cnt;
// while loops for each case
}
}
Is there any better approach to solve this problem in O(n2) or better ?
As your array is of size N*N and the desired computation demands each element to be visited atleast once, there cannot be a solution better than O(n^2) which uses 2 dimensional arrays.
I think that your solution will be fine if the operation has to be done single time on the same array.
If you have to do this operation multiple times on the same input array, better create circles from the input array. The data structure of circle should be a CLL (circular linked list). So doing the operation multiple times will be piece of cake as you have to change the root element of the CLL storing the circle info depending on the direction.
I think that this can be solved easily in-place in O(n) time.
(NOTE: O(N) where N is the total number of matrix elements)
The following solution doesn't use four consecutive loops, but uses a small table of [X, Y] deltas that describe the difference between the current coordinate and the next one, when the next coordinate is invalid (i.e outside of the current window) the index to the table itself is advanced and the look-up repeated. The algorithm starts with full window and decreases it by one element from each side every time the nested loop finishes. The whole process repeats until the window defined as [minX, minY, maxX, maxY] is valid (i.e. contains at least 2x2 elements).
This solution doesn't implement swapping cw and ccw, but this is the easiest part to add.
function pad(s, n) {
while(s.length < n)
s = " " + s;
return s;
}
// Create a matrix of [WxH] size.
function Matrix(w, h, data) {
if (Array.isArray(data)) {
if (data.length !== w * h)
throw new Error("Data.length has to match the size " + (w * h) + ".");
}
else {
var n = typeof data === "number" ? data : 0.0;
data = [];
for (var i = 0; i < w*h; i++) data.push(n);
}
this.w = w;
this.h = h;
this.data = data;
}
// Get value at [x, y]
Matrix.prototype.get = function(x, y) {
if (x < 0 || x >= this.w || y < 0 || y >= this.h)
throw new Error("Index [" + x + ", " + y + "] out of bounds");
return this.data[y * this.w + x];
}
// Set value at [x, y] and return the previous value.
Matrix.prototype.set = function(x, y, value) {
if (x < 0 || x >= this.w || y < 0 || y >= this.h)
throw new Error("Index [" + x + ", " + y + "] out of bounds");
var i = y * this.w + x;
var prev = this.data[i];
this.data[i] = value;
return prev;
}
// Log the matrix data.
Matrix.prototype.dump = function() {
var s = "["
var i = 0;
for (var y = 0; y < this.h; y++) {
for (var x = 0; x < this.w; x++, i++) {
s += pad("" + this.data[i], 2);
if (x !== this.w - 1)
s += ","
}
if (y !== this.h - 1)
s += ",\n ";
}
s += "]";
console.log(s);
}
// Shift, `dir` can be "cw" or "ccw".
Matrix.prototype.shift = function(dir) {
var instructions = {
cw : [1, 0, 0, 1,-1, 0, 0,-1],
ccw: [0, 1, 1, 0, 0,-1,-1, 0]
};
var inst = instructions[dir];
// A window, shrink by one from each side after the nested loop is done.
var minX = 0;
var minY = 0;
var maxX = this.w - 1;
var maxY = this.h - 1;
while (minX < maxX && minY < maxY) {
// Always start at the top-left corner and iterate.
var x0 = minX;
var y0 = minY;
var v0 = this.get(x0, y0);
var n = 0;
for (;;) {
var x1 = x0 + inst[n + 0];
var y1 = y0 + inst[n + 1];
if (x1 < minX || x1 > maxX || y1 < minY || y1 > maxY) {
n += 2;
x1 = x0 + inst[n + 0];
y1 = y0 + inst[n + 1];
}
v0 = this.set(x1, y1, v0);
// Last one.
if (x1 === minX && y1 === minY)
break;
x0 = x1;
y0 = y1;
}
minX++;
minY++;
maxX--;
maxY--;
}
}
var a = new Matrix(3, 3, [
1,2,3,
4,5,6,
7,8,9,
]);
a.dump();
a.shift("cw");
a.dump();
var b = new Matrix(4, 4, [
1 ,2 ,3 ,4 ,
5 ,6 ,7 ,8 ,
9 ,10,11,12,
13,14,15,16
]);
b.dump();
b.shift("ccw");
b.dump();
I faced this problem recently in a job interview and I failed to solve it in under one hour.
Then I got home and produced the code below in java. It is recursive and I believe it has O(n^2) complexity. You can also see the code here: https://github.com/lotif/rotateMatrix
You will have first to input the dimension of the (square) matrix and then the numbers of the matrix itself separated by spaces, line by line. For instance:
3
1 2 3
4 5 6
7 8 9
It will return you the matrix rotated clockwise in concentric circles.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class RotateMatrix {
public static void main(String args[] ) throws Exception {
Scanner s = new Scanner(System.in);
int d = s.nextInt();
int[][] matrix = new int[d][d];
for(int i = 0; i < d; i++) {
for(int j = 0; j < d; j++) {
matrix[i][j] = Integer.parseInt(s.next());
}
}
matrix = rotate(matrix);
for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix.length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
s.close();
}
public static int[][] rotate(int[][] matrix) {
if(matrix == null || matrix.length == 0 || matrix.length == 1) {
return matrix;
}
List<Integer> outerCircle = getOuterCircle(matrix);
matrix = removeOuterCircle(matrix);
//rotating outer circle
outerCircle.add(0, outerCircle.remove(outerCircle.size() - 1));
matrix = rotate(matrix);
matrix = addOuterCircle(outerCircle, matrix);
return matrix;
}
private static int[][] addOuterCircle(List<Integer> outerCircle, int[][] matrix) {
int d = matrix.length + 2;
int[][] newMatrix = new int[d][d];
//Adding the outer circle to the matrix
for(int j = 0; j < d; j++) {
newMatrix[0][j] = outerCircle.remove(0);
}
for(int i = 1; i < d; i++) {
newMatrix[i][d-1] = outerCircle.remove(0);
}
for(int j = d-2; j >= 0; j--) {
newMatrix[d-1][j] = outerCircle.remove(0);
}
for(int i = d-2; i >= 1; i--) {
newMatrix[i][0] = outerCircle.remove(0);
}
//Adding the inner matrix
for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix[i].length; j++) {
newMatrix[i + 1][j + 1] = matrix[i][j];
}
}
return newMatrix;
}
private static List<Integer> getOuterCircle(int[][] matrix) {
int d = matrix.length;
List<Integer> outerCircle = new ArrayList<Integer>();
for(int j = 0; j < d; j++) {
outerCircle.add(matrix[0][j]);
}
for(int i = 1; i < d; i++) {
outerCircle.add(matrix[i][d-1]);
}
for(int j = d-2; j >= 0; j--) {
outerCircle.add(matrix[d-1][j]);
}
for(int i = d-2; i >= 1; i--) {
outerCircle.add(matrix[i][0]);
}
return outerCircle;
}
private static int[][] removeOuterCircle(int[][] matrix) {
int d = matrix.length;
int[][] newMatrix = new int[d-2][d-2];
for(int i = 1; i < d-1; i++) {
for(int j = 1; j < d-1; j++) {
newMatrix[i-1][j-1] = matrix[i][j];
}
}
return newMatrix;
}
}
public class ShiftArray {
static void shiftArray(int[][]a, int index, int n) {
if ((n%2 == 0) && (index >= n/2))
return;
if ((n%2 != 0) && (index > n/2))
return;
int tempRowTopLast = a[index][n-1-index];
int tempColRightLast = a[n-1-index][n-1-index];
int tempRowBottomLast = a[n-1-index][index];
int tempColLeftLast = a[index][index];
int temp, temp2;
temp = tempColLeftLast;
for (int k = index + 1; k < n-index; k++) {
temp2 = a[index][k];
a[index][k] = temp;
temp = temp2;
}
temp = tempRowTopLast;
for (int k = index + 1; k < n-index; k++) {
temp2 = a[k][n-1-index];
a[k][n-1-index] = temp;
temp = temp2;
}
temp = tempColRightLast;
for (int k = n-2-index; k >=index; k--) {
temp2 = a[n-1-index][k];
a[n-1-index][k] = temp;
temp = temp2;
}
temp = tempRowBottomLast;
for (int k = n-2-index; k >=index; k--) {
temp2 = a[k][index];
a[k][index] = temp;
temp = temp2;
}
shiftArray(a, index+1, n);
}
public static void main(String[] args) {
int a[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
shiftArray(a, 0, 3);
System.out.println("Rotated array...");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(a[i][j] + ",");
}
System.out.println();
}
}
}
import java.util.Scanner;
public class RotateMatrix
{
static int rows = 0;
static int cols = 0;
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
rows = scan.nextInt();
cols = scan.nextInt();
int rots = scan.nextInt();
int[][] matrix = new int[rows][cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
matrix[i][j] = scan.nextInt();
}
}
for (int i = 0; i < rots; i++)
rotate(matrix, 0, rows - 1, 0, cols - 1);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
scan.close();
}
public static int[][] rotate(int[][] arr, int rowStart, int rowEnd, int colStart, int colEnd)
{
if (rowStart == rowEnd && colStart == colEnd)
{
return arr;
}
if (rowStart > rowEnd || colStart > colEnd)
{
return arr;
}
int temp = arr[rowStart][colStart];
for (int j = colStart; j < colEnd; j++)
{
arr[colStart][j] = arr[colStart][j + 1];
}
for (int i = rowStart; i < rowEnd; i++)
{
arr[i][colEnd] = arr[i + 1][colEnd];
}
for (int i = colEnd; i > colStart; i--)
{
arr[rowEnd][i] = arr[rowEnd][i - 1];
}
for (int i = rowEnd; i > rowStart; i--)
{
arr[i][colStart] = arr[i - 1][colStart];
}
if (rows == 1)
{
arr[colEnd][rowStart] = temp;
}
else
arr[rowStart + 1][colStart] = temp;
System.out.println("-----------------------------------------\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
System.out.println("-----------------------------------------\n");
rotate(arr, rowStart + 1, rowEnd - 1, colStart + 1, colEnd - 1);
return arr;
}
}
Clockwise rotation by 90 degrees. O(n^2) time and O(1) memory in Python3:
# #param A : list of list of integers
# #return the same list modified
def rotate(A):
for row in range(len(A) // 2):
for col in range(row, len(A)-1 - row): # First col already takes care of last.
r = row
c = col
tmp1 = A[r][c]
while True:
next_r = c
next_c = len(A) - 1 - r
tmp2 = A[next_r][next_c]
A[next_r][next_c] = tmp1
if next_r == row and next_c == col:
break
tmp1 = tmp2
r = next_r
c = next_c
return A

Resources