I’ve been following a video on YouTube where there is a tutorial on working with Points and Line Segments. After several days of building on this project suddenly a core section of my code fails and I have no idea why…
I’m easily 10 classes deep at this point and after tracing the data back from the error, it boils down to this code…
Thank you for your reply. I understand that the check for equality is failing because it is returning undefined. What I don’t understand is why the data that is pulled from localStorage is failing in the first place.
I have tried introducing a tolerance in the equality to see if perhaps floating point is introducing the error… but as it stands… p1 remains undefined…
static load() {
const data = JSON.parse(localStorage.getItem('graph'), Graph.reviver);
if (!data || !Array.isArray(data.points) || !Array.isArray(data.segments)) {
console.error('Invalid or missing data in localStorage.');
return null; // Or handle the error in a way that fits your needs
}
const points = data.points.map((pointData) => new Point(pointData.x, pointData.y));
const findPoint = (targetPoint) => {
return points.find((p) => p.equals(targetPoint, 5)); // Adjust tolerance as needed
};
const segments = data.segments.map((segmentData) => {
console.log('Segment data:', segmentData);
// Log the entire points array
console.log('All points:', points);
// Attempt to find p1
const p1 = findPoint(segmentData.p1);
// Log the specific point that should be found
console.log('Expected p1:', segmentData.p1);
// Log the found p1
console.log('Found p1:', p1);
// Attempt to find p2
const p2 = findPoint(segmentData.p2);
// Log the specific point that should be found
console.log('Expected p2:', segmentData.p2);
// Log the found p2
console.log('Found p2:', p2);
if (p1 && p2) {
return new Segment(p1, p2);
} else {
console.error('Invalid segment data:', segmentData);
return null;
}
});
// Filter out null segments if any (due to errors in data)
const validSegments = segments.filter((segment) => segment !== null);
console.log('Loaded points:', points);
console.log('Loaded segments:', validSegments);
return new Graph(points, validSegments);
}
Is there another way to access the values of the Points other than map and find ?
At the moment, these changes now at least draw the first segment and fail on the second… I can create a new line but an error is thrown where point.draw() is not a function…
Try this, because i’m trying to interpret where your problem is coming from specifically.
const segments = info.segments.map((i) => {
console.log(`Trying to find: ${i.p1.x}, ${i.p1.y} in points: `+JSON.stringify(points));
Throw the last line of that console output before the error in here. (If this line BECOMES the error, there’s a problem in your info, meaning whatever’s load’ing your info has bad data).
Well this point does not exist in your set of points. So yes, it will fail to find anything. Either you’ve somehow told it to search for something that doesnt exist, or your set of points is incomplete. So… onward to figure out which of those directions has misfired?
draw(ctx) {
for (const segment of this.segments) {
if (segment instanceof Segment) {
segment.draw(ctx);
}
}
for (const point of this.points) {
point.draw(ctx);
}
}
I’m… going to guess from the error message being “this.p2.equals”, your error is occuring in Segment’s draw function…
A Point would not have a “p2”, the only thing that should have a p2 is a segment.
So something is calling includes, which… unless you’ve got something else calling it, would be equals… so something called a Segment object’s equals.
If i had to guess… it’s bacwards from here:
What’s the definition of ctx’s beginPath code? what is ctx?
I suspect the stack trace is currently…
Segment.includes()
Segment.equals()
(Unknown Type) ctx.beginPath()
Segment.draw()
Graph.draw()
GraphEditor.display()
So it’s a little unclear to me which of those calls the draw a Segment is the one triggering the error. I can only assume it is the base Segment draw() and not the others down the line…