QwtPlotCurve doesn't show up on graph - c++

I'm currently working on a project in which I load a huge number of data points on a graph (something like 50,000, so I can zoom in as much as I want).
I wanted to test how the commands worked, so I thought I'd try out the code with 10 pieces of data, but unfortunately my curve refuses to show up on my graph.
QwtPlot *leftGraph;
leftGraph = new QwtPlot;
leftGraph->setCanvasBackground(Qt::white);
leftGraph->setMaximumHeight(200);
leftGraph->setAxisScale(0, 0.0, 20.0, 2.0);
leftGraph->setAxisScale(2, 0.0, 20.0, 2.0);
and
QwtPlotCurve *curve = new QwtPlotCurve();
curve->setStyle(QwtPlotCurve::Lines);
curve->setCurveAttribute(QwtPlotCurve::Fitted, true);
const double x[] = {0, 1, 2, 4, 5, 8, 10, 13, 14, 19};
const double y[] = {17, 16.5, 8, 3, 5, 7.5, 9, 10, 12, 14};
curve->setSamples(x, y, 10);
curve->attach(leftGraph);
Any ideas? Many thanks.

Try calling leftGraph->replot() to make the curve appear.

Related

How to assign value to dart enum like in c++

I'm trying to adapt a c++ to dart, and I ran into this situation with enum, assigning default values ​​I think. follow the code
enum skills_t : uint8_t {
SKILL_FIST = 0,
SKILL_CLUB = 1,
SKILL_SWORD = 2,
SKILL_AXE = 3,
SKILL_DISTANCE = 4,
SKILL_SHIELD = 5,
SKILL_FISHING = 6,
SKILL_CRITICAL_HIT_CHANCE = 7,
SKILL_CRITICAL_HIT_DAMAGE = 8,
SKILL_LIFE_LEECH_CHANCE = 9,
SKILL_LIFE_LEECH_AMOUNT = 10,
SKILL_MANA_LEECH_CHANCE = 11,
SKILL_MANA_LEECH_AMOUNT = 12,
SKILL_MAGLEVEL = 13,
SKILL_LEVEL = 14,
SKILL_FIRST = SKILL_FIST,
SKILL_LAST = SKILL_MANA_LEECH_AMOUNT
};
}
uint32_t skillBase[SKILL_LAST + 1] = {50, 50, 50, 50, 30, 100, 20};
Is it possible to adapt this code to dart/flutter?
I would like to replicate the same operation in dart, it seems that he assigned these values ​​to each enum in a range
Yes, it is possible to adapt this code to Dart/Flutter.
In Dart, you can use the enum keyword to define an enumeration. The syntax is similar to C++, but there is no need to specify a type like uint8_t.
Regarding the default values, you can initialize the enum members with a value like in C++.
Here is an example of how the C++ code could be adapted to Dart:
enum Skills {
FIST,
CLUB,
SWORD,
AXE,
DISTANCE,
SHIELD,
FISHING,
CRITICAL_HIT_CHANCE,
CRITICAL_HIT_DAMAGE,
LIFE_LEECH_CHANCE,
LIFE_LEECH_AMOUNT,
MANA_LEECH_CHANCE,
MANA_LEECH_AMOUNT,
MAGLEVEL,
LEVEL,
FIRST = FIST,
LAST = MANA_LEECH_AMOUNT,
}
final List<int> skillBase = [
50, 50, 50, 50, 30, 100, 20
];
You can also use a Map to assign the default values to each enum member.
enum Skills {
FIST,
CLUB,
SWORD,
AXE,
DISTANCE,
SHIELD,
FISHING,
CRITICAL_HIT_CHANCE,
CRITICAL_HIT_DAMAGE,
LIFE_LEECH_CHANCE,
LIFE_LEECH_AMOUNT,
MANA_LEECH_CHANCE,
MANA_LEECH_AMOUNT,
MAGLEVEL,
LEVEL,
FIRST = FIST,
LAST = MANA_LEECH_AMOUNT,
}
final Map<Skills, int> skillBase = {
Skills.FIST: 50,
Skills.CLUB: 50,
Skills.SWORD: 50,
Skills.AXE: 50,
Skills.DISTANCE: 30,
Skills.SHIELD: 100,
Skills.FISHING: 20,
// Add the rest of the skills
};
Both the above examples will work fine in dart/flutter.

How to reset two-dimensional array to its original form after sorting?

I am trying to reset 2D array to its original form after it's been sorted with a bubble sort. I need to reset it back to what it was before sorting. How do I do it? In case you have a question why an array is global. It's a school assignment and that's how our professor wants us to do it.
Here is my program:
#include<iostream>
using namespace std;
const int NUM_COLS=4;
const int NUM_ROWS=5;
int array[NUM_ROWS][NUM_COLS]={{5, 3, 2, 16},
{9, 8, 10, 17},
{4, 7, 11, 18},
{2, 5, 9, 12},
{7, 9, 4, 10}};
it sorts an array with bubbleSort
void bubbleSort(int row, int col){}
it is display array function header
void displayArray(){}
and here is main function
int main(){
cout<<"original array"<<endl;
displayArray();
bubbleSort(NUM_ROWS-1, NUM_COLS);
cout<<"\nbubble sort"<<endl;
displayArray();
reset();
displayArray();
return 0;
}
Now I need to reset an array back to original. I did this but it doesn't work.
void reset(){
int array[NUM_ROWS][NUM_COLS]={{5, 3, 2, 16},
{9, 8, 10, 17},
{4, 7, 11, 18},
{2, 5, 9, 12},
{7, 9, 4, 10}};
}
Your reset is declaring a new array (and doing nothing with it). You can't assign (=) C style arrays, so you will need something that looks different. If you can use std::array instead, you could assign in reset.
#include <array>
const int NUM_COLS=4;
const int NUM_ROWS=5;
std::array<std::array<int, NUM_ROWS>, NUM_COLS> values = {
{5, 3, 2, 16},
{9, 8, 10, 17},
{4, 7, 11, 18},
{2, 5, 9, 12},
{7, 9, 4, 10}};
// Other code probably remains unchanged
void reset() {
values = {
{5, 3, 2, 16},
{9, 8, 10, 17},
{4, 7, 11, 18},
{2, 5, 9, 12},
{7, 9, 4, 10}};
}
At which point you notice that you've got your bounds the wrong way round, and it should either be
const int NUM_COLS=5;
const int NUM_ROWS=4;
or a differently shaped array initialiser.
void reset(){
static int original[NUM_ROWS][NUM_COLS]={{5, 3, 2, 16},
{9, 8, 10, 17},
{4, 7, 11, 18},
{2, 5, 9, 12},
{7, 9, 4, 10}};
for (int i = 0; i < NUM_ROWS; i++)
memcpy(array[i], original[i], NUM_COLS * sizeof(int));
}
Not the prettiest thing, but this should work. Since that's how your professor wants you to do it, go for it ¯\_(ツ)_/¯
As I said in a comment, the easiest way to assign arrays is to wrap them in a structure. Voilà, suddenly C++ develops abilities it didn't even know it had inherited from C and copies arrays!1 Even nested, multi-dimensional arrays!
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
const int NUM_COLS=4;
const int NUM_ROWS=5;
// Define a struct (i.e., a class with all public members)
// which has just a single member, the array. Note that this is
// only a *type* declaration, no object is created yet.
struct arrT
{
int array [NUM_ROWS][NUM_COLS];
};
// object creation.
arrT workArr;
void reset()
{
// The initialization value is hidden inside the function.
// static variables are initialized only once, for constant
// data at compile time.
static const arrT oriArr
{
{ {5, 3, 2, 16},
{9, 8, 10, 17},
{4, 7, 11, 18},
{2, 5, 9, 12},
{7, 9, 4, 10}
}
};
workArr = oriArr; // simple default assignment of structs
}
// The parameters are redundant.
void stdSort(int /*row*/, int /*col*/)
{
// Sort the 2D array as a one-dimensional sequence
// (which the elements are in memory).
// The algorithm expects iterators to the first and
// one-after-the-last elements in the sequence. Pointers
// to the elements in an array are perfectly good iterators.
std::sort(&workArr.array[0][0], &workArr.array[NUM_ROWS-1][NUM_COLS]);
}
void displayArray()
{
// The top-level elements of a 2D array are the rows...
for(auto &row: workArr.array)
{
// ... and the elements of the rows are ints.
// Note how the
// dimensions are known from the class declaration.
for(auto &el: row)
{
cout << setw(4) << el;
}
cout << "\n";
}
}
int main(){
cout << "Work array before initialization:\n";
displayArray();
reset(); // before, the values of the global array are 0.
cout<<"\nWork array after init:\n";
displayArray();
stdSort(NUM_ROWS, NUM_COLS);
cout<<"\nWork array after std sort"<<endl;
displayArray();
reset();
cout << "\nWork array after reset\n";
displayArray();
return 0;
}
1 Arrays are the only example I know of off the cuff where the memberwise assignment of the generated default assignment operator can assign a type which does not have a standalone assignment operator (which is the exact reason we jump through this hoop). Are there others?

Arduino Icon Library

I'm working on an Arduino project and I want to display icons on a NeoMatrix 8x8 panel.
Initially, I went in direction that relied on inheritance, and requested some input on Arduino Stack Exchange where I was advised to go another route, and to ask my question elsewhere is it pertained more to C++ than to Arduino.
Instead of relying on inheritance, I was recommended to store my icons in PROGMEM and have the program build the icons from there.
I attempted the approach as much as I could, but I'm not at ease so I'd like some further insight!
The idea is to have a byte array that composes an 8x8 icon.
The values are 0-2, each representing a color set in an RGB struct array.
From what I believe I understand, my byte array stored in PROGMEM is read as a pointer, and needs to be accessed using ppm_read_byte.
I am unsure on how to handle the RGB struct. When I attempt to read it from PROGMEM, it causes my program to crash. So I removed it from PROGMEM, and the icon displays correctly. My byte array is in PROGMEM, but not the colors.
I am aware that I have a serious lack of knowledge about pointers which I need to work on...
Also, the idea is to have a collection of icons, so should I store all my icons (byte arrays and colors) in a header file? Will that not bloat it?
Thank you in advance for insight on this!
header.h
typedef struct {
byte r;
byte g;
byte b;
} RGB;
const byte PROGMEM WifiIcon[8][8] = {
{1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 2, 2, 2, 2, 1, 1},
{1, 2, 1, 1, 1, 1, 2, 1},
{2, 1, 2, 2, 2, 2, 1, 2},
{1, 2, 1, 1, 1, 1, 2, 1},
{1, 1, 1, 2, 2, 1, 1, 1},
{1, 1, 2, 1, 1, 2, 1, 1},
{0, 0, 0, 1, 1, 0, 0, 0}
};
const RGB WifiIconColors[3] = {
{0, 0, 0},
{0, 0, 0},
{0, 200, 61}
};
ESP8266Neomatrix.ino
#include "header.h"
void printIcon(int startPosition, const byte (&icon)[8][8], const RGB (&colors)[3]){
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
byte currentPixel = pgm_read_byte(&icon[i][j]);
const RGB currentColor = colors[currentPixel];
byte red = currentColor.r;
byte green = currentColor.g;
byte blue = currentColor.b;
matrix.setPixelColor(startPosition++, red, green, blue);
}
}
}
https://gist.github.com/Nate1661/0eea9200e9d1c86187c2acf205ba3602
If you want the RGB data to reside in PROGMEM, since it is not a native type handled by pgm_read_XXX functions, just read it with memcpy_P():
RGB currentColor;
memcpy_P(&currentColor, colors + currentPixel, sizeof(RGB));
If this crashes, then perhaps there is an issue with the value of currentPixel that you read.

All pushed back values in vector changing to default value

In an effort to reduce multiple functions (that were nearly identical) in my code, I decided to consolidate them all into one function which takes an additional parameter (a class with multiple parameters, actually), and then uses those values to imitate what the multiple functions would have done. Then, long story short, I put each of those class declarations into a vector, and now my program seems dysfunctional.
My multiple instances of a class:
FragmentCostParameters commonFCP = FragmentCostParameters(.05, 0);
FragmentCostParameters rareFCP = FragmentCostParameters(.05, 50);
FragmentCostParameters uniqueFCP = FragmentCostParameters(.05, 125);
FragmentCostParameters legendaryFCP = FragmentCostParameters(.02, 175);
FragmentCostParameters crystallineFCP = FragmentCostParameters(.02, 250);
FragmentCostParameters superEliteFCP = FragmentCostParameters(.02, 300);
Which get placed into a vector by:
vector<FragmentCostParameters> FCPs(6);
FCPs.push_back(FragmentCostParameters(.05, 0));
FCPs.push_back(FragmentCostParameters(.05, 50));
FCPs.push_back(FragmentCostParameters(.05, 125));
FCPs.push_back(FragmentCostParameters(.02, 175));
FCPs.push_back(FragmentCostParameters(.02, 250));
FCPs.push_back(FragmentCostParameters(.02, 300));
Additionally, that class is defined below:
class FragmentCostParameters {
public:
double multFactor;
double subtractFactor;
FragmentCostParameters(double _multFactor, double _subtractFactor){
multFactor = _multFactor;
subtractFactor = _subtractFactor;
}
FragmentCostParameters(){
multFactor = .05;
subtractFactor = 0;
}
};
Now, you'll notice that the default constructor for the FragmentCostParameters involves setting multFactor = .05 and subtractFactor = 0. However, it seems that no matter what I push back, each of the values in my vector become mutated into those values. At least, that's what VS 2011 tells me the values are equal to when I'm looking at them in a local scope in the following function (which is the only place they're used).
int FragmentCost(double skillLevel, int duration, FragmentCostParameters FCP){
return max((int)(ceil(FCP.multFactor*(skillLevel-FCP.subtractFactor))*ceil(duration/30.0)) , 0);
}
And the only place that FragmentCost is called is from this function below, which is supposed to pass different values .. but somewhere in the process, when I look at locals in FragmentCost, they're always the default values in the constructor for the class.
//given a skill level + duration, will return an array with the fragment usage
int* regularTotalFragCost(int skillLevel, int duration){
int fragments[7] = {0,0,0,0,0,0,0};
fragments[0]+= FragmentCost(skillLevel,duration, FCPs[0]);
fragments[1]+= FragmentCost(skillLevel,duration, FCPs[0]);
fragments[0]+= FragmentCost(skillLevel,duration, FCPs[1]);
fragments[2]+= FragmentCost(skillLevel,duration, FCPs[1]);
fragments[0]+= FragmentCost(skillLevel,duration, FCPs[2]);
fragments[3]+= FragmentCost(skillLevel,duration, FCPs[2]);
fragments[0]+= FragmentCost(skillLevel,duration, FCPs[3]);
fragments[4]+= FragmentCost(skillLevel,duration, FCPs[3]);
fragments[0]+= FragmentCost(skillLevel,duration, FCPs[4]);
fragments[5]+= FragmentCost(skillLevel,duration, FCPs[4]);
fragments[0]+= FragmentCost(skillLevel,duration, FCPs[5]);
fragments[6]+= FragmentCost(skillLevel,duration, FCPs[5]);
return fragments;
}
For some reason I feel that I'm making a really stupid mistake somewhere, but for the life of me I can't seem to figure it out. I would appreciate any help and/or advice anyone could offer.
EDIT: Here's what the values for fragments[] in regularTotalFragCost are supposed to be if everything is working correctly, using a couple test values (skillLevel = 250 and duration = 30)
FCPs[0] : Fragments: 13, 13, 0, 0, 0, 0, 0,
FCPs[1] : Fragments: 17, 15, 2, 0, 0, 0, 0,
FCPs[2] : Fragments: 20, 14, 5, 1, 0, 0, 0,
FCPs[3] : Fragments: 29, 14, 9, 5, 1, 0, 0,
FCPs[4] : Fragments: 32, 13, 10, 7, 2, 0, 0,
FCPs[5] : Fragments: 32, 13, 10, 7, 2, 0, 0,
And here is what they are as of right now:
FCPs[0] : Fragments: 78, 13, 13, 13, 13, 13, 13,
FCPs[1] : Fragments: 78, 13, 13, 13, 13, 13, 13,
FCPs[2] : Fragments: 78, 13, 13, 13, 13, 13, 13,
FCPs[3] : Fragments: 78, 13, 13, 13, 13, 13, 13,
FCPs[4] : Fragments: 78, 13, 13, 13, 13, 13, 13,
FCPs[5] : Fragments: 78, 13, 13, 13, 13, 13, 13,
vector<FragmentCostParameters> FCPs(6);
creates a vector of 6 default-constructed values, numbered 0-5.
FCPs.push_back(FragmentCostParameters(.05, 0));
adds value at index 6
int fragments[7] is a local variable on the stack. Once that function returns, that memory is no longer valid. You are returning a pointer to that local variable and any access to that memory is undefined behavior.
Instead, return an std::array:
std::array<int, 7> fragments = {}; // value initialize
return fragments;
Hint: How big is your vector after you push_back your FCPs?

double free or corruption error c++

i have a code for a graphic engine, it need to draw wire frames and line drawings, i made some adjustments to te original code of mine and now i get the error double free or corruption, but before the code worked just fine, does annybody know what i'm doing wrong?
void Wireframe::Generate(list<eye_point> &points, list<line> &lines, const ini::Configuration &configuration)
{
string buffer;
stringstream out;
for(int i = 0; i < nrFigures; i++)
{
figure = "Figure";
out<<i;
buffer = out.str();
figure.append(buffer);
out.str(string());
cout<<"de figure heeft de naam "<<figure<<endl;
Read_info(configuration);
Generate_points(points, configuration);
Generate_lines(lines, configuration);
}
}
in read info he reads the info from the ini file
void Wireframe::Generate_points(list<eye_point> &points, const ini::Configuration &configuration){
Matrix schaal = Scale(scale);
Matrix translate = Translatie(center);
Matrix xrotate = Rotate_x_as(rotatex);
Matrix yrotate = Rotate_y_as(rotatey);
Matrix zrotate = Rotate_z_as(rotatez);
Matrix eyematrix = Eye_transformatie(eye);
Matrix matrix;
matrix = schaal * translate * xrotate * yrotate * zrotate * eyematrix;
if(type.compare("LineDrawing") == 0)
{
linedrawing_point(points, configuration, matrix);
}
else if(type.compare("Cube") == 0)
{
cube_point(points,matrix);
}
}
void Wireframe::Generate_lines(list<line> &lines, const ini::Configuration &configuration){
if(type.compare("LineDrawing") == 0)
{
linedrawing_lines(lines, configuration);
}
else if (type.compare("Cube") == 0)
{
cube_lines(lines);
}
}
here he sees wha for line drawing he needs to do, were by line drawing works just fine, the error is in cube.
void Wireframe::cube_lines(list<line> &lines){
getline(lines, 1, 5);
getline(lines, 5, 3);
getline(lines, 3, 7);
getline(lines, 7, 1);
getline(lines, 5, 2);
getline(lines, 2, 8);
getline(lines, 8, 3);
getline(lines, 3, 5);
getline(lines, 2, 6);
getline(lines, 6, 4);
getline(lines, 4, 8);
getline(lines, 8, 2);
getline(lines, 6, 1);
getline(lines, 1, 7);
getline(lines, 7, 4);
getline(lines, 4, 6);
getline(lines, 7, 3);
getline(lines, 3, 8);
getline(lines, 8, 4);
getline(lines, 4, 7);
getline(lines, 1, 6);
getline(lines, 6, 2);
getline(lines, 2, 5);
getline(lines, 5, 1);
}
void Wireframe::cube_point(list<eye_point> &points, Matrix &matrix){
getpoint(1, -1, -1, points, 1, matrix );
getpoint(-1, 1, -1, points, 2, matrix );
getpoint(1, 1, 1, points, 3, matrix );
getpoint(-1, -1, 1, points, 4, matrix );
getpoint(1, 1, -1, points, 5, matrix );
getpoint(-1, -1, -1, points, 6, matrix );
getpoint(1, -1, 1, points, 7, matrix );
getpoint(-1, 1, 1, points, 1, matrix );
}
void Wireframe::projectie(Vector3D &vector_points, eye_point &point_element){
point_element.z = vector_points.z;
if(vector_points.z != 0)
{
point_element.x = vector_points.x / -vector_points.z;
point_element.y = vector_points.y / -vector_points.z;
}
else
{
point_element.x = vector_points.x;
point_element.y = vector_points.y;
}
}
void Wireframe::getpoint(double x, double y, double z, list<eye_point> &points, int nummer, Matrix &matrix ){
eye_point point_element;
Vector3D vector_points = Vector3D::point(x, y, z);
vector_points *= matrix;
point_element.figure = figure;
point_element.punt = nummer;
projectie(vector_points, point_element);
points.push_back(point_element);
}
void Wireframe::getline(list<line> &lines, int lijn0, int lijn1){
line line_element;
line_element.lijn0 = lijn0;
line_element.lijn1 = lijn1;
line_element.figure = figure;
line_element.linecolor = linecolor;
lines.push_back(line_element);
}
If you are on Windows, you might want to try AppVerifier tool which is free and is designed to detect double free errors http://msdn.microsoft.com/en-us/library/ms807121.aspx
The code posted doesn't directly do any allocation or freeing, so it's not relevant to your bug.
It's likely that the objects you're putting into containers (line and eye_point) have a bug. For instance, missing an assignment operator or copy constructor could lead to all sorts of baffling behavior.