I have below code:
<image
id="wheel-bg"
class="wheel-bg"
:x="-width/2"
:y="-height/2"
href="images/wheel-bg.png"
:width="width"
:height="height"
filter="drop-shadow(black 0px 0px 1rem)"
#click="spin"
></image>
I have a data property "rotationState" which can be "false" or "true".
rotationState = false means #click event should fire and
rotationState = true means #click event should not fire
I tried
#click="spin && rotationState"
I also tried:
#click.stop="rotationState"
#click="spin"
and
:disabled="rotationState"
but above methods don't work because I think I have #click event on an <image></image> element.
What I am trying to achieve is basically,
container.on('click', null); this is the code that was used when I first wrote the code in jquery / js.
Jquery Spin function code
container.on("click", spin); //this code triggers spin in jquery
function spin(d) {
container.on("click", null); //this code disables multiple spin from happening
spinCount++;
// First 2 spins
if (spinCount < totalSpins) {
//get random number of rotations + get random degree for "No Win"
// 1 to 28 or 211 to 239 deg
if (Math.random() < 0.5) {
rotation = ((Math.floor(Math.random() * 1) + 3) * 360) + (Math.floor(Math.random() * 28) + 1);
} else {
rotation = ((Math.floor(Math.random() * 1) + 3) * 360) + (Math.floor(Math.random() * 28) + 211);
}
duration = 10000; // 10 seconds
// Third spin
} else if (spinCount == totalSpins) {
//get random number of rotations + get random degree for options other than "No Win" and "Free Apple Earpods"
//31 to 119 deg or 151 to 209 deg or 271 to 359
if (Math.random() < 0.7) {
rotation = ((Math.floor(Math.random() * 1) + 5) * 360) + (Math.floor(Math.random() * 88) + 31);
} else if (Math.random() > 0.3 && Math.random() <= 0.7) {
rotation = ((Math.floor(Math.random() * 1) + 5) * 360) + (Math.floor(Math.random() * 58) + 151);
} else {
rotation = ((Math.floor(Math.random() * 1) + 5) * 360) + (Math.floor(Math.random() * 88) + 271);
}
duration = 15000; // 15 seconds
// Spins more than 3
} else {
alert("No more spins left");
container.on("click", null);
return;
}
updateAttempts(spinCount);
updateSpins(totalSpins - spinCount);
//Get selected array element
var degSelected = (rotation % 360) % 360;
if (
(degSelected >= 91 && degSelected <= 120) ||
(degSelected >= 181 && degSelected <= 210) ||
(degSelected >= 331 && degSelected <= 360)
) {
selected = 11;
} else if (
(degSelected >= 1 && degSelected <= 30) ||
(degSelected >= 121 && degSelected <= 150) ||
(degSelected >= 181 && degSelected <= 210) ||
(degSelected >= 211 && degSelected <= 240)
) {
selected = 10;
}else if (
(degSelected >= 31 && degSelected <= 60) ||
(degSelected >= 151 && degSelected <= 180) ||
(degSelected >= 271 && degSelected <= 300)
) {
selected = 9;
}else if (degSelected >= 241 && degSelected <= 270) {
selected = 6;
}else if (
(degSelected >= 61 && degSelected <= 90) ||
(degSelected >= 301 && degSelected <= 330)
) {
selected = 4;
}
vis.transition()
.duration(duration)
.ease(d3.easeExpOut)
.attrTween("transform", rotTween)
.each("end", function () {
//set prize value
prize = data[selected].value;
id = data[selected].id;
if(spinCount == 3) {
alert('Your Voucher: ' + data[selected].label);
}else {
alert('Better luck next time');
}
oldrotation = rotation;
container.on("click", spin);
});
img2.transition()
.duration(duration)
.ease(d3.easeExpOut)
.attrTween("transform", rotTween)
}
You can set the function of #click, based on the state of rotationState as mentioned before:
#click="rotationState ? spin() : null"
Related
I'm implementing in code an algorithm for computing inverse polynomials in the NTRU cryptosystem, and I'm using the paper "Almost Inverses and Fast NTRU Key Creation" by Joseph H. Silverman. I implemented the second pseudo-code as:
int inverse_mod_p(polynomial *r, polynomial *a)
{
int k;
int16_t b[NTRU_N + 1], c[NTRU_N + 1], f[NTRU_N + 1], g[NTRU_N + 1];
int i;
int16_t aux;
int zero_f;
int constant_f;
int deg_fg;
memset(b, 0, (NTRU_N + 1) * sizeof(int16_t));
b[0] = 1;
memset(c, 0, (NTRU_N + 1) * sizeof(int16_t));
memcpy(f, a->coeffs, NTRU_N * sizeof(int16_t));
f[NTRU_N] = 0;
memset(g, 0, (NTRU_N + 1) * sizeof(int16_t));
g[0] = -1;
g[NTRU_N] = 1;
while (1)
{
zero_f = 1;
for (i = 0; i < NTRU_N + 1; i++)
{
if (f[i] != 0)
{
zero_f = 0;
break;
}
}
if (zero_f)
return 1;
while (f[0] == 0)
{
for (i = 0; i < NTRU_N; i++)
{
f[i] = f[i + 1];
c[NTRU_N - i] = c[NTRU_N - i - 1];
}
f[NTRU_N] = 0;
c[0] = 0;
k++;
}
constant_f = 1;
for (i = 1; i < NTRU_N + 1; i++)
{
if (f[i] != 0)
{
constant_f = 0;
break;
}
}
if (constant_f)
break;
deg_fg = 0;
for (i = NTRU_N; i >= 0; i--)
{
if (f[i] == 0 && g[i] != 0)
{
deg_fg = 1;
break;
}
else if (f[i] != 0 && g[i] == 0)
{
break;
}
}
if (deg_fg)
{
for (i = 0; i < NTRU_N + 1; i++)
{
aux = f[i];
f[i] = g[i];
g[i] = aux;
aux = b[i];
b[i] = c[i];
c[i] = aux;
}
}
if (f[0] == g[0])
{
for (i = 0; i < NTRU_N + 1; i++)
{
f[i] = (f[i] - g[i] + 3) % 3;
b[i] = (b[i] - c[i] + 3) % 3;
}
}
else
{
for (i = 0; i < NTRU_N + 1; i++)
{
f[i] = (f[i] + g[i] + 3) % 3;
b[i] = (b[i] + c[i] + 3) % 3;
}
}
}
k = k % NTRU_N;
for (i = NTRU_N - 1; i >= 0; i--)
{
if (i - k < 0)
r->coeffs[i - k + NTRU_N] = b[i] * f[0];
else
r->coeffs[i - k] = b[i] * f[0];
}
for (i = 0; i < NTRU_N; i++)
r->coeffs[i] = (r->coeffs[i] + 3) % 3;
return 0;
}
But this seems to be wrong. I tested it using the example giveng in Wikipedia: https://en.wikipedia.org/wiki/NTRUEncrypt . The polynomial -1 + x + x^2 - x^4 + x^6 + x^9 - x^10 should have as inverse the polynomial 1 + 2x + 2x^3 + 2x^4 + x^5 + 2x^7 + x^8 - x^10 , but I got the following result:
Polinomial:
-1 1 1 0 -1 0 1 0 0 1 -1
Inverse polinomial:
0 2 2 1 0 2 1 2 0 1 2
Where is the error in the implementation?
I have created this if and else if statements. Flat, Vertical, Facedown, Right Side, and Left Side work... However, I am not sure why the Upside down is not working. Here is my code:
checkOrientation= () => {
let orientation = "unknown";
if (z > .8 && Math.abs(x) < .2 && Math.abs(y) < .2) orientation = "flat";
else if (y > .9 && Math.abs(x) < .2 &&Math.abs(z) < .2) orientation = "vertical";
if (x < .1 && Math.abs(y) < .1 && Math.abs(z) < .3) orientation = "right side";
else if (z < .01 && Math.abs(x) > .9 && Math.abs(y) < .05) orientation = "left side";
if (x > .01 && Math.abs(y) < .7 && Math.abs(z) < .05) orientation = "upside down";
else if (x > .01 && Math.abs (y) < .01 && Math.abs(z) > .01) orientation = "face down";
this.setState({orientation});
}
Following situation:
i want to build a cgrect. what i need is: up to 4 rows. up to 6 columns.
so for example how it have to look:
i need min.
+ + + +
(1 row, 4 buttons)
and max i need:
+ + + + + +
+ + + + +
+ + + + + +
+ + + + +
(4 rows, 22 buttons)
what i want is to pass the BUTTONS_FOR_ROW1-4 data from an other VC. for the min example this is button_for_row1 = 4, button_for_row2 = 0, button_for_row3 = 0, button_for_row4 = 0.
for the max example button_for_row1 = 6, button_for_row2 = 5, button_for_row3 = 6, button_for_row4 = 5
my code now is this:
-(void) generateCardViews {
int positionsLeftInRow = _BUTTONS_PER_ROW;
int j = 0; // j = ROWNUMBER (j = 0) = ROW1, (j = 1) = ROW2...
for (int i = 0; i < [self.gameModel.buttons count]; i++) {
NSInteger value = ((ButtonModel *)self.gameModel.buttons[i]).value;
CGFloat x = (i % _BUTTONS_PER_ROW) * 121 + (i % _BUTTONS_PER_ROW) * 40 + 285;
if (j == 1) {
x += 80; // set additional indent (horizontal displacement)
}
if (j == 2) {
x -= 160;
}
CGFloat y = j * 122 + j * 40 + 158;
CGRect frame = CGRectMake(x, y, 125, 125);
ButtonView *cv = [[ButtonView alloc] initWithFrame:frame andPosition:i andValue:value];
if (!((ButtonModel *)self.gameModel.buttons[i]).outOfPlay) {
[self.boardView addSubview:cv];
if ([self.gameModel.turnedButtons containsObject: self.gameModel.buttons[i]]) {
[self.turnedButtonViews addObject: cv];
[cv flip];
}
}
if (--positionsLeftInRow == 0) {
j++;
positionsLeftInRow = _BUTTONS_PER_ROW;
if (j == 1) {
positionsLeftInRow = _BUTTONS_PER_ROW-1;
if (j == 2) {
positionsLeftInRow = _BUTTONS_PER_ROW-2;
}}
}
}
}
As you can see my code now have just BUTTONS_PER_ROW and not BUTTONS_FOR_ROW1 - 4.
the indent is for pushing a row left or right.
But i think this will work much easier, cause with my code i get some weird things when i make 22 Buttons.
thanks for help!
Your basic logic can be simplified. I would use a while loop here. For the case of a maximum number of buttons per row (the simpler case), this is an outline of what I think is a clear way to do what you want:
y = .... (initial value of y)
NSUInteger numberOfButtons = [self.gameModel.buttons count];
// Layout out the buttons, with no more than _BUTTONS_PER_ROW in each row
NSUInteger buttonsLeft = numberOfButtons;
NSUInteger buttonsInRow = 0;
while(buttonsLeft>0)
{
if(buttonsInRow>_BUTTONS_PER_ROW)
{
// Increment y, reset x
y += ....
x = .... (initial position for x)
buttonsInRow = 0;
}
// Create a button
CGRect frame = CGRectMake(x, y, 125, 125);
ButtonView* buttonView = [[ButtonView .....
x += 80;
++buttonsInRow;
--buttonsLeft;
}
For the more general case, add a variable to keep track of the row number, and use and array with the maximum number of buttons per row that is loaded before entering the while loop.
How do you use the waveshapernode in the web audio api? particular the curve Float32Array attribute?
Feel free to look at an example here.
In detail, I create a waveshaper curve with this function:
WAAMorningStar.prototype.createWSCurve = function (amount, n_samples) {
if ((amount >= 0) && (amount < 1)) {
ND.dist = amount;
var k = 2 * ND.dist / (1 - ND.dist);
for (var i = 0; i < n_samples; i+=1) {
// LINEAR INTERPOLATION: x := (c - a) * (z - y) / (b - a) + y
// a = 0, b = 2048, z = 1, y = -1, c = i
var x = (i - 0) * (1 - (-1)) / (n_samples - 0) + (-1);
this.wsCurve[i] = (1 + k) * x / (1+ k * Math.abs(x));
}
}
Then "load" it in a waveshaper node like this:
this.createWSCurve(ND.dist, this.nSamples);
this.sigmaDistortNode = this.context.createWaveShaper();
this.sigmaDistortNode.curve = this.wsCurve;
Everytime I need to change the distortion parameter, I re-create the waveshaper curve:
WAAMorningStar.prototype.setDistortion = function (distValue) {
var distCorrect = distValue;
if (distValue < -1) {
distCorrect = -1;
}
if (distValue >= 1) {
distCorrect = 0.985;
}
this.createWSCurve (distCorrect, this.nSamples);
}
(I use distCorrect to make the distortion sound nicer, values found euristically).
You can find the algorithm I use to create the waveshaper curve here
I am trying to convert an image into polaroid filter in the following way:
int m_width = self.size.width;
int m_height = self.size.height;
uint32_t *rgbImage = (uint32_t *) malloc(m_width * m_height * sizeof(uint32_t));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(
rgbImage,
m_width,
m_height,
8,
m_width * 4,
colorSpace,
kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast
);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetShouldAntialias(context, NO);
CGContextDrawImage(context, CGRectMake(0, 0, m_width, m_height), [self CGImage]);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
// now convert to grayscale
uint8_t *m_imageData = (uint8_t *) malloc(m_width * m_height);
for(int y = 0; y < m_height; y++)
{
for(int x = 0; x < m_width; x++)
{
uint32_t rgbPixel=rgbImage[y*m_width+x];
uint32_t redIn = 0, greenIn = 0,blueIn = 0,max = 0,min = 0,chroma = 0,hue = 0,saturation = 0,redOut = 0,greenOut = 0,blueOut = 0;
redIn = (rgbPixel>>24)&255;
greenIn = (rgbPixel>>16)&255;
blueIn = (rgbPixel>>8)&255;
//NSLog(#"redIn %u greenIn %u blueIn %u",redIn,greenIn,blueIn);
max = redIn > greenIn ? redIn > blueIn ? redIn : blueIn : greenIn > blueIn ? greenIn : blueIn;
min = redIn < greenIn ? redIn < blueIn ? redIn : blueIn : greenIn < blueIn ? greenIn : blueIn;
chroma = max - min;
if(chroma != 0)
{
if(max == redIn)
hue = ((greenIn - blueIn) / chroma) %6 ;
else if(max == greenIn)
hue = ((blueIn - redIn)/chroma) + 2;
else if(max == blueIn)
hue = ((redIn - greenIn)/chroma) + 4;
hue = (hue+20)%6;
if(chroma != 0)
saturation = chroma / max;
if(hue >= 0 && hue < 1)
{
redOut = chroma + max - chroma;
greenOut = chroma * (1 - abs((hue % 2) - 1)) + max - chroma;
blueOut = 0 + max - chroma;
}
else if(hue >= 1 && hue < 2)
{
redOut = chroma * (1 - abs((hue % 2) - 1)) + max - chroma;
greenOut = chroma + max - chroma;
blueOut = 0 + max - chroma;
}
else if(hue >= 2 && hue < 3)
{
redOut = 0 + max - chroma;
greenOut = chroma + max - chroma;
blueOut = chroma * (1 - abs((hue % 2) - 1)) + max - chroma;
}
else if(hue >= 3 && hue < 4)
{
redOut = 0 + max - chroma;
greenOut = chroma * (1 - abs((hue % 2) - 1)) + max - chroma;
blueOut = chroma + max - chroma;
}
else if(hue >= 4 && hue < 5)
{
redOut = chroma * (1 - abs((hue % 2) - 1)) + max - chroma;
greenOut = 0 + max - chroma;
blueOut = chroma + max - chroma;
}
else if(hue >= 5 && hue < 6)
{
redOut = chroma + max - chroma;
greenOut = 0 + max - chroma;
blueOut = chroma * (1 - abs((hue % 2) - 1)) + max - chroma;
}
}
//NSLog(#"redOut %u greenOut %u blueOut %u",redOut,greenOut,blueOut);
m_imageData[y*m_width+x]=redOut + greenOut + blueOut;
}
}
free(rgbImage);
// convert from a gray scale image back into a UIImage
uint8_t *result = (uint8_t *) calloc(m_width * m_height *sizeof(uint32_t), 1);
// process the image back to rgb
for(int i = 0; i < m_height * m_width; i++)
{
result[i*4]=0;
int val=m_imageData[i];
result[i*4+1]=val;
result[i*4+2]=val;
result[i*4+3]=val;
}
free(m_imageData);
// create a UIImage
colorSpace = CGColorSpaceCreateDeviceRGB();
context = CGBitmapContextCreate(result,
m_width,
m_height,
8,
m_width * sizeof(uint32_t),
colorSpace,
kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast
);
CGImageRef image = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
#ifdef kNYXReturnRetainedObjects
UIImage* resultUIImage = [[UIImage alloc] initWithCGImage:image];
#else
UIImage *resultUIImage = [UIImage imageWithCGImage:image];
#endif
CGImageRelease(image);
free(result);
return resultUIImage;
but I'm not getting the polaroid image after execution this code.
What's the problem in this code or can you suggest me some other way.
The most obvious problem with the code is that, at the end of the method, you create a drawing context, do nothing in it, then take an image from it and return that as a UIImage. This will be blank, presumably. I think you're trying to obtain the image that is held in the data in result - I think you need to look at CGImageCreate instead rather than setting up a new context.
See here for information on creating a data provider with raw data, which you will need to pass to CGImageCreate. This all does look rather complicated.