why am i getting expected primary-expression before '.' token [closed] - struct

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 15 hours ago.
Improve this question
I am trying to create a Struct with two structs inside. One struct has a 2d array in it. The other struct packs 2 versions of the previous struct. I have a working code. However, in this code I have this statement:
`if (lane_bank_select == 0) {
for (uint8_t i = 0; i < lane_amount; i = i + 1) {
Serial.print(db.data_lane_bank_1[i].data_1);
Serial.print(" ");
Serial.print(db.data_lane_bank_1[i].data_2);
Serial.print(" ");
Serial.print(db.data_lane_bank_1[i].data_3);
Serial.print(" ");
Serial.println(db.data_lane_bank_1[i].data_4);
}
} else if (lane_bank_select == 1) {
for (uint8_t i = 0; i < lane_amount; i = i + 1) {
Serial.print(db.data_lane_bank_2[i].data_1);
Serial.print(" ");
Serial.print(db.data_lane_bank_2[i].data_2);
Serial.print(" ");
Serial.print(db.data_lane_bank_2[i].data_3);
Serial.print(" ");
Serial.println(db.data_lane_bank_2[i].data_4);
}
} else {
Serial.print(db.data_lane_bank_1[0].data_1);
Serial.print(" ");
Serial.print(db.data_lane_bank_1[0].data_2);
Serial.print(" ");
Serial.print(db.data_lane_bank_1[0].data_3);
Serial.print(" ");
Serial.println(db.data_lane_bank_1[0].data_4);
}`
I would like to change this statement to something like this.
`for (uint8_t i = 0; i < memory_amount; i = i + 1) {
if (lane_bank_select == i) {
for (uint8_t j = 0; j < bank_amount; j = j + 1) {
for (uint8_t k = 0; k < lane_amount; k = k + 1) {
for (uint8_t l = 0; l < data_amount; l = l + 1) {
Serial.print(db.data_lane_bank[j][k].data[l]);
Serial.println(" ");
}
}
}
}
}`
So i wrote this:
`const uint8_t memory_amount = 2;
const uint8_t bank_amount = 2;
const uint8_t data_amount = 4;
const uint8_t lane_amount = 4;
const uint8_t lane_array_size = 4;
uint8_t lane_bank_select = 0; // This is used to select the data lane bank
struct data_lane { // creates 4 variables for the data lane.
uint8_t data[data_amount];
};
// packs 2 versions of data_lane struct into one struct
struct data_bank {
data_lane data_lane_bank[bank_amount][lane_amount];
};
void setup() {
Serial.begin(9600);
}
void loop() {
// use serial port to select data_lane bank via lane_bank_select
serial_lane_select();
data_bank db = {
.data_lane_bank[0] = {
{ 1, 2, 3, 4 }, // Data_lane_1
{ 11, 12, 13, 14 }, // Data_lane_2
{ 21, 22, 23, 24 }, // Data_lane_3
{ 31, 32, 33, 34 }, // Data_lane_4
},
.data_lane_bank[1] = {
{ 41, 42, 43, 44 }, // Data_lane_1
{ 51, 52, 53, 54 }, // Data_lane_2
{ 61, 62, 63, 64 }, // Data_lane_3
{ 71, 72, 73, 74 }, // Data_lane_4
}
};
for (uint8_t i = 0; i < memory_amount; i = i + 1) {
if (lane_bank_select == i) {
for (uint8_t j = 0; j < bank_amount; j = j + 1) {
for (uint8_t k = 0; k < lane_amount; k = k + 1) {
for (uint8_t l = 0; l < data_amount; l = l + 1) {
Serial.print(db.data_lane_bank[j][k].data[l]);
Serial.println(" ");
}
}
}
}
}
}
void serial_lane_select() {
uint8_t lane_recieved;
if (Serial.available() > 0) {
lane_recieved = Serial.read();
if (lane_recieved == '1') // Single Quote! This is a character.
{
lane_bank_select = 0;
}
if (lane_recieved == '2') {
lane_bank_select = 1;
}
}
}`
I am now getting this error:
expected primary-expression before '.' token .data_lane_bank[0] =
Can somebody help please.

Related

Google Code Jam: My solution to Train Timetable problem is failing

I'm trying to solve this problem from Google's Code Jam 2008:
The problem is called Train Timetable and you can find the full explanation here:
Code Jam - Train Timetable
Note: I've decided to solve the problem with Node.js.
My code is the next:
function timeToMinutes(time) {
const timeArray = time.split(":");
const hours = parseInt(timeArray[0]);
const minutes = parseInt(timeArray[1]);
const hoursInMinutes = hours * 60;
const total = hoursInMinutes + minutes;
return total;
}
function timetableFiller(NAB, NBA, array) {
let timetable = {
departuresFromA: [],
arrivalsToB: [],
departuresFromB: [],
arrivalsToA: [],
};
for (let i = 0; i < NAB + NBA; i++) {
let tempArr = [];
tempArr = array[i].split(" ");
if (i < NAB) {
timetable.departuresFromA.push(tempArr[0]);
timetable.arrivalsToB.push(tempArr[1]);
} else {
timetable.departuresFromB.push(tempArr[0]);
timetable.arrivalsToA.push(tempArr[1]);
}
}
return timetable;
}
function timetableToMinutes(timetable) {
let timetableMinutes = {
departuresFromA: [],
arrivalsToB: [],
departuresFromB: [],
arrivalsToA: [],
};
for (const property in timetable) {
timetable[property].map((element) =>
timetableMinutes[property].push(timeToMinutes(element))
);
}
return timetableMinutes;
}
function trainsNeededCounter(arrivalsFromDestiny, departuresFromOrigin, tat) {
let trainsNeeded = departuresFromOrigin.length;
for (let i = 0; i < arrivalsFromDestiny.length; i++) {
for (let j = 0; j < departuresFromOrigin.length; j++) {
if (arrivalsFromDestiny[i] + tat <= departuresFromOrigin[j]) {
trainsNeeded = trainsNeeded - 1;
departuresFromOrigin.splice(j, 1);
}
}
}
return trainsNeeded;
}
function responseGenerator(inputA, inputB, caseNumber) {
return `Case #${caseNumber}: ${inputA} ${inputB}`;
}
function problemSolution(input) {
const numberOfCases = parseInt(input[0]);
input.shift();
let response = [];
let caseNumber = 0;
let NAB;
let NBA;
for (let i = 0; i < input.length; i = i + NAB + NBA + 2) {
caseNumber = caseNumber + 1;
const tat = parseInt(input[i]);
const arrayNTrips = input[i + 1].split(" ");
NAB = parseInt(arrayNTrips[0]);
NBA = parseInt(arrayNTrips[1]);
const arraySchedule = input.slice(i + 2, i + 2 + NAB + NBA);
const timetable = timetableFiller(NAB, NBA, arraySchedule);
const timetableMinutes = timetableToMinutes(timetable);
const trainsNeededAB = trainsNeededCounter(
timetableMinutes.arrivalsToA,
timetableMinutes.departuresFromA,
tat
);
const trainsNeededBA = trainsNeededCounter(
timetableMinutes.arrivalsToB,
timetableMinutes.departuresFromB,
tat
);
response.push(
responseGenerator(trainsNeededAB, trainsNeededBA, caseNumber)
);
}
return response;
}
function readInput() {
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false,
});
let problem = [];
rl.on("line", (line) => {
problem.push(line);
}).on("close", () => {
const solution = problemSolution(problem);
solution.map((response) => console.log(response));
});
}
readInput();
How to replicate the issue
You should login into Code Jam with your Google account.
Paste into the code area on the right side and activate the Test run mode.
As input you can copy paste the sample input provided in the problem and you can see that the output is exactly as the sample output.
I've tried with my own variations of the input and the responses seems correct but when I run the real attempt the platform says WA or Wrong Answer.
Thank you so much for your help!
I made a video about this recently. You should check it out.
I think you can understand the logic flow from it. We are both doing the same thing basically.
https://youtu.be/_Cp51vMDZAs
-check this out
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void solve(int t)
{
int NA, NB;
float T;
cin >> T >> NA >> NB;
cin.ignore();
vector<string> ASchedule, BSchedule;
if (NA > 0)
for (int i = 0; i < NA; i++)
{
string s;
getline(cin, s);
ASchedule.push_back(s);
}
if (NB > 0)
for (int i = 0; i < NB; i++)
{
string s;
getline(cin, s);
BSchedule.push_back(s);
}
int alength, blength;
alength = (int)ASchedule.size();
blength = (int)BSchedule.size();
if (alength == 0 || blength == 0)
{
cout << "Case #" << t << ": " << alength << " " << blength << endl;
return;
}
float TT = T / 10;
string val, value;
int d;
float ADH, ADM, AAH, AAM, BDH, BDM, BAH, BAM;
vector<float> AD, AA, BD, BA;
for (int i = 0; i < alength; i++)
{
val = ASchedule[i];
ADH = stof(val.substr(0, 2));
AAH = stof(val.substr(6, 2));
ADM = stof(val.substr(3, 2));
AAM = stof(val.substr(9, 2));
if (val.at(9) == '0')
{
AAM /= 10;
AAM += TT;
AAM *= 10;
}
else
AAM += T;
if (AAM > 59)
{
d = -1;
while (AAM != 59)
{
AAM -= 1;
d++;
}
AAH++;
AAM = 0;
AAM += d;
}
// if (ADH > 23)
// ADH = 0;
// if (AAH > 23)
// AAH = 0;
ADM /= 100;
ADH += ADM;
AAM /= 100;
AAH += AAM;
AD.push_back(ADH);
AA.push_back(AAH);
}
for (int j = 0; j < blength; j++)
{
value = BSchedule[j];
BDH = stof(value.substr(0, 2));
BDM = stof(value.substr(3, 2));
BAH = stof(value.substr(6, 2));
BAM = stof(value.substr(9, 2));
if (value.at(9) == '0')
{
BAM /= 10;
BAM += TT;
BAM *= 10;
}
else
BAM += T;
if (BAM > 59)
{
d = -1;
while (BAM != 59)
{
BAM -= 1;
d++;
}
BAH++;
BAM = 0;
BAM += d;
}
// if (BDH > 23)
// BDH = 0;
// if (BAH > 23)
// BAH = 0;
BDM /= 100;
BDH += BDM;
BAM /= 100;
BAH += BAM;
BA.push_back(BAH);
BD.push_back(BDH);
}
int no1 = alength, no2 = blength;
sort(BD.begin(), BD.end());
sort(BA.begin(), BA.end());
sort(AA.begin(), AA.end());
sort(AD.begin(), AD.end());
for (int i = 0; i < alength; i++)
for (int j = 0; j < blength; j++)
if (AD[i] >= BA[j])
{
no1--;
BA[j] = 50;
break;
}
for (int i = 0; i < blength; i++)
for (int j = 0; j < alength; j++)
if (AA[j] <= BD[i])
{
no2--;
AA[j] = 50;
break;
}
cout << "Case #" << t << ": " << no1 << " " << no2 << endl;
}
int main()
{
int N;
cin >> N;
cin.ignore();
for (int t = 1; t <= N; t++)
solve(t);
}

P5.JS Sketch Won't Load in Browser

I have a simple program, but I have an issue. I previously had a problem with the page displaying "Loading..." before, but I fixed that. Whenever I try to load now, I get "localhost refused to connect". I have tried the internet but all the fixes seem to be for the first error. How do I get it to load? Here is the code:
let pew;
let crosshair;
let imgConst = 100;
var imgSize = imgConst;
var imgChange = [4, (6 + 2/3), 10, -10, -10, -10, -10, -20, (-33 - 1/3), -50];
for(var i = 0; i = imgChange.length; i++) {
imgChange[i] = imgSize + imgConst/imgChange[i]
console.log(i)
}
function preload() {
crosshair = loadImage('crosshair.png')
}
function setup() {
createCanvas(windowWidth, windowHeight);
frameRate(10);
noStroke();
noCursor();
pew = new bullet();
}
function draw() {
background(50, 89, 100);
//Shrink bullets
pew.shrink()
//crosshair
image(crosshair, mouseX - imgSize/2, mouseY - imgSize/2, imgSize, imgSize);
}
// bullet class
class bullet {
constructor() {
this.x = [];
this.y = [];
this.size = [];
this.shrinkSpeed = 1;
this.diameter = 10;
}
shrink() {
fill(61, 41, 15)
for(var i = 0; i < this.x.length; i++) {
if(this.size[i] <= 1) {
this.size.splice(i);
} else {
ellipse(this.x[i], this.y[i], this.size[i], this.size[i]);
this.size[i] = this.size[i] - this.shrinkSpeed;
}
}
}
add() {
this.x.push(mouseX);
this.y.push(mouseY);
this.size.push(this.diameter)
for( i = 0; i < imgChange.length * 1; i ++) {
imgSize = imgChange[floor(i)]
console.log(imgSize);
}
}
}
function mousePressed() {
//add bullet
pew.add();
}
You have 2 problems in for loops.
First, in your first for loop, change i = imgChange.length to i < imgChange.length. Second, you are missing a var in the add() function of your bullet class: for( i = 0.
Now, your code should run.

How can you get a sprite to follow another sprite in Java?

Here is the code for the initial character:
public class Player extends Mob {
private InputHandler input;
private int colour = Colours.get(-1, 111, 145, 543);
private int scale = 1;
protected boolean isSwimming = false;
private int tickCount = 0;
public Player(Level level, int x, int y, InputHandler input) {
super(level, "Player", x, y, 1);
this.input = input;
}
public void tick() {
int xa = 0;
int ya = 0;
if (input.up.isPressed()) {
ya--;
}
if (input.down.isPressed()) {
ya++;
}
if (input.left.isPressed()) {
xa--;
}
if (input.right.isPressed()) {
xa++;
}
if (xa != 0 || ya != 0) {
move(xa, ya);
isMoving = true;
} else {
isMoving = false;
}
if (level.getTile(this.x >> 3, this.y >> 3).getId() == 3) {
isSwimming = true;
}
if (isSwimming && level.getTile(this.x >> 3, this.y >> 3).getId() != 3) {
isSwimming = false;
}
tickCount++;
this.scale = 1;
}
public void render(Screen screen) {
int xTile = 0;
int yTile = 28;
int walkingSpeed = 4;
int flipTop = (numSteps >> walkingSpeed) & 1;
int flipBottom = (numSteps >> walkingSpeed) & 1;
if (movingDir == 1) {
xTile += 2;
} else if (movingDir > 1) {
xTile += 4 + ((numSteps >> walkingSpeed) & 1) * 2;
flipTop = (movingDir - 1) % 2;
}
int modifier = 8 * scale;
int xOffset = x - modifier / 2;
int yOffset = y - modifier / 2 - 4;
if (isSwimming) {
int waterColour = 0;
yOffset += 4;
if (tickCount % 60 < 15) {
waterColour = Colours.get(-1, -1, 225, -1);
} else if (15 <= tickCount % 60 && tickCount % 60 < 30) {
yOffset -= 1;
waterColour = Colours.get(-1, 225, 115, -1);
} else if (30 <= tickCount % 60 && tickCount % 60 < 45) {
waterColour = Colours.get(-1, 115, -1, 225);
} else {
waterColour = Colours.get(-1, 225, 115, -1);
}
screen.render(xOffset, yOffset + 3, 0 + 27 * 32, waterColour, 0x00, 1);
screen.render(xOffset + 8, yOffset + 3, 0 + 27 * 32, waterColour, 0x01, 1);
}
// upper body
screen.render(xOffset + (modifier * flipTop), yOffset, xTile + yTile * 32, colour, flipTop, scale);
screen.render(xOffset + modifier - (modifier * flipTop), yOffset, (xTile + 1) + yTile * 32, colour, flipTop,
scale);
if (!isSwimming) {
// lower body
screen.render(xOffset + (modifier * flipBottom), yOffset + modifier, xTile + (yTile + 1) * 32, colour,
flipBottom, scale);
screen.render(xOffset + modifier - (modifier * flipBottom), yOffset + modifier,
(xTile + 1) + (yTile + 1) * 32, colour, flipBottom, scale);
}
}
public boolean hasCollided(int xa, int ya) {
int xMin = 0;
int xMax = 7;
int yMin = 1;
int yMax = 7;
for (int x = xMin; x < xMax; x++) {
if (isSolidTile(xa, ya, x, yMin)) {
return true;
}
}
for (int x = xMin; x < xMax; x++) {
if (isSolidTile(xa, ya, x, yMax)) {
return true;
}
}
for (int y = yMin; y < yMax; y++) {
if (isSolidTile(xa, ya, xMin, y)) {
return true;
}
}
for (int y = yMin; y < yMax; y++) {
if (isSolidTile(xa, ya, xMax, y)) {
return true;
}
}
return false;
}
}
can get another character to load at a given location; however, I cannot figure out an algorithm to have it follow the initial player. In other words the second one will follow these same collision and commands. Any help understanding is appreciated, thank you!

How to read non-byte-aligned integers with Node.js?

I am trying to read a binary SWF file using Node.JS. As the specification mentions at the bottom of the 17th page, some integers are encoded using variable-length bit fields, and by definition most of them are not byte-aligned.
The problem is that Node.js's Buffer only provides functions for reading byte-aligned integers. So I tried to write a wrapper object that would read bit by bit. However, it seems really hacky. Below is the object prototype I wrote:
/*jslint node:true, bitwise:true */
'use strict';
var util = require('util');
var maxBits = 32;
function BitBuffer() {
Buffer.apply(this, arguments);
this.cursor = 0;
this.bitCursor = 0;
}
util.inherits(BitBuffer, Buffer);
module.exports = BitBuffer;
function padBits(bits, length, bit) {
var leftPad = '', repeatLength, i;
bits = bits.toString(2);
length = length || 8;
bit = bit || '0';
if (bits.length >= length) {
return bits;
} else {
repeatLength = length - bits.length;
for (i = 0; i < repeatLength; i += 1) {
leftPad += bit;
}
return leftPad + bits;
}
}
BitBuffer.prototype.move = function (bits) {
var bytes = Math.floor((this.bitCursor + bits) / 8);
this.bitCursor += bits;
if (this.bitCursor > 8) {
this.cursor += bytes;
this.bitCursor -= bytes * 8;
}
if (this.cursor >= this.length) {
this.rewind();
return false;
}
return true;
};
BitBuffer.prototype.align = function () {
if (this.bitCursor > 0) {
this.bitCursor = 0;
this.cursor += 1;
}
};
BitBuffer.prototype.rewind = function () {
this.cursor = this.bitCursor = 0;
};
BitBuffer.prototype.readBits = function (bits) {
var bytes = Math.ceil((this.bitCursor + bits) / 8), result = 0,
length, buffer, i;
if (bits > maxBits) {
throw new RangeError('Cannot read more than ' + maxBits + ' bits');
}
buffer = this.slice(this.cursor, this.cursor + bytes);
result = padBits(buffer[0]).substr(this.bitCursor);
length = buffer.length;
for (i = 1; i < length; i += 1) {
result += padBits(buffer[i]);
}
result = result.substr(0, bits);
return (this.move(bits)) ? parseInt(result, 2) : false;
};
BitBuffer.prototype.readUB = BitBuffer.prototype.readBits;
BitBuffer.prototype.readSB = function (bits) {
var readBits = this.readBits(bits),
stringBits = readBits.toString(2);
if (readBits === false) {
return false;
}
// add automatically removed zeros
if (stringBits.length < bits) {
stringBits = padBits(stringBits, bits);
}
// negative, pad to 32 bits then invert bits
if (stringBits[0] === '1') {
return -~parseInt(padBits(stringBits, maxBits, '1'), 2) - 1;
} else {
return readBits;
}
};
BitBuffer.prototype.readFB = function (bits) {
var highBits, lowBits, result;
if (bits < 17) {
throw new RangeError('Should be at least 17 bits long');
}
highBits = this.readSB(bits - 16);
lowBits = this.readUB(16);
lowBits *= Math.pow(10, -lowBits.toString(10).length);
return (highBits >= 0) ?
highBits + lowBits : highBits - lowBits;
};
// wrap read functions
(function () {
var nativeReadFunctions = {
'readInt8': 8,
'readInt16LE': 16,
'readInt16BE': 16,
'readInt32LE': 32,
'readInt32BE': 32,
'readUInt8': 8,
'readUInt16LE': 16,
'readUInt16BE': 16,
'readUInt32LE': 32,
'readUInt32BE': 32,
'readDoubleLE': 64,
'readDoubleBE': 64,
'readFloatLE': 32,
'readFloatBE': 32
}, method;
function wrapNativeRead(method, length) {
return function (noAssert) {
var cursor;
this.align();
cursor = this.cursor;
this.move(length);
return Buffer.prototype[method].call(
this,
this.cursor,
noAssert
);
};
}
for (method in nativeReadFunctions) {
if (nativeReadFunctions.hasOwnProperty(method)) {
BitBuffer.prototype[method] =
wrapNativeRead(method, nativeReadFunctions[method]);
}
}
}());
Is writing my own object the good way?
Beside your implementation is pretty good, you should understand that in your implementation this.buffer === this and you should change the following lines:
// inside BitBuffer prototype constructor
this.buffer = new Buffer(param1, param2);
// change to
Buffer.call(this, param1, param2); // The right way to call the super constructor
and
// inside BitBuffer.prototype.readBits
buffer = this.buffer.slice(this.cursor, this.cursor + bytes);
// change to
buffer = this.slice(this.cursor, this.cursor + bytes);

Fractal generation from infinite sum

I found a project description on a course website for computer graphics. I am trying to complete the project for fun.
Here is the link to the problem description:
http://www.pdfhost.net/index.php?Action=Download&File=901bc7785bef41364b3a40f6f4493926
Below is my code. The problem I am running in to is that the terms of the series grow so fast I can't map the points to the screen correctly. From the problem description it says the points will be mappable within a -2 - 2 square but the difference in value between the points is so huge that normalizing by the largest would collapse most of the points to a single pixel.
I assume I have a fundamental misunderstanding that I can't identify. Any help or insight would be appreciated!
int w = 800, h = 600;
int numTimes = 10, cSize = 5;
float xr = 2, yr = 2;
void setup() {
size(w,h);
}
void draw() {
background(255);
Complex v = new Complex(mouseX*(xr/w) - (xr/2), mouseY*(yr/h) - (yr/2));
Complex[] exps = new Complex[numTimes];
for (int i = 0; i < numTimes; i++) {
exps[i] = complexExp(v,i);
}
ellipse(w/2, h/2, cSize, cSize);
for (int i = 0; i < numTimes; i++) {
drawSeries(new Complex(0,0), exps, i, i);
}
}
void drawSeries(Complex vToDraw, Complex[] exps, int count, int clrTrunc) {
if (count == 0) {
Complex v = exps[0];
float progress = float(clrTrunc) / float(numTimes);
fill(255*progress, 180, 255 - 255*progress);
vToDraw.add(v);
ellipse(vToDraw.r*(w/xr) + (w/2), vToDraw.i*(h/xr) + h/2, cSize, cSize);
vToDraw.sub(v);
vToDraw.sub(v);
ellipse(vToDraw.r*(w/xr) + (w/2), vToDraw.i*(h/xr) + h/2, cSize, cSize);
} else {
Complex v = exps[count];
vToDraw.add(v);
drawSeries(vToDraw, exps, count - 1, clrTrunc );
vToDraw.sub(v);
vToDraw.sub(v);
drawSeries(vToDraw, exps, count - 1,clrTrunc );
}
}
Complex complexExp(Complex v, int times) {
if (times == 0) {
return new Complex(1, 1);
} else if ( times == 1) {
return new Complex( v.r*v.r - v.i*v.i, 2*v.r*v.i );
} else {
return complexExp( new Complex( v.r*v.r - v.i*v.i, 2*v.r*v.i ), times - 1 );
}
}
class Complex {
float r, i;
Complex() {
this.r = 0;
this.i = 0;
}
Complex(float r, float i) {
this.r = r;
this.i = i;
}
void add(Complex nv) {
this.r += nv.r;
this.i += nv.i;
}
void sub(Complex nv) {
this.r -= nv.r;
this.i -= nv.i;
}
}
I think you can make the code cleaner if you write a more complete Complex class.
int w = 800, h = 600;
int numTimes = 10, cSize = 5;
float xr = 3, yr = 3;
void setup() {
size(w,h);
noLoop();
}
void mousePressed() {
redraw();
}
void draw() {
background(255);
Complex v = new Complex(mouseX*(xr/w) - (xr/2), mouseY*(yr/h) - (yr/2));
Complex[] exps = new Complex[numTimes];
for (int i = 0; i < numTimes; i++) {
exps[i] = v.raisedTo(i);
print(exps[i]);
}
ellipse(w/2, h/2, cSize, cSize);
print(exps);
drawSerie(exps, numTimes);
}
void drawSerie(Complex[] exps, int total)
{
Complex partial = new Complex(0, 0);
drawPartial(exps, total -1, partial);
}
void drawFinal(Complex toDraw)
{
point(toDraw.r*(w/xr) + (w/2), toDraw.i*(h/xr) + h/2);
}
void drawPartial(Complex [] exps, int depth, Complex partial)
{
if (depth == -1)
{
drawFinal(partial);
return;
}
int nextDepth = depth -1;
drawPartial(exps, nextDepth, partial);
Complex element = exps[depth];
drawPartial(exps, nextDepth, partial.add(element));
drawPartial(exps, nextDepth, partial.sub(element));
}
class Complex {
float r, i;
Complex() {
this.r = 0;
this.i = 0;
}
Complex(float r, float i) {
this.r = r;
this.i = i;
}
Complex(Complex other)
{
this.r = other.r;
this.i = other.i;
}
Complex mult(Complex other)
{
return new Complex(this.r*other.r - this.i*other.i, this.r*other.i + this.i*other.r);
}
Complex add(Complex nv) {
return new Complex(this.r + nv.r, this.i + nv.i);
}
Complex sub(Complex nv) {
return new Complex(this.r - nv.r, this.i - nv.i);
}
Complex raisedTo(int n) {
if (n == 0) {
return new Complex(1, 0);
}
else if (n % 2 == 0)
{
return (this.mult(this)).raisedTo(n/2);
}
else
{
return this.mult(this.raisedTo(n - 1 ));
}
}
String toString()
{
return "real: " + this.r + " imaginary: " + this.i;
}
}
The computation of the series is not efficient but, I think, it is clear

Resources