-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix ellipseMode(CORNERS) and rectMode(CORNER) #7290
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f43d12d
Fix ellipseMode(CORNERS) and rectMode(CORNER)
martinleopold 4e08593
Update test for helpers/modeAdjust
martinleopold 89dd45f
Add visual tests for shape modes
martinleopold f510d31
Merge branch 'main' into ellipse-fix
martinleopold c6fd362
Reduce size of test sketches
martinleopold 2d18e61
Workaround for false negatives for exactly matching images
martinleopold baa5f9d
Updated contributer info
martinleopold File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
Helper function that draws a shape using the specified shape mode | ||
p5 ............... The p5 Instance | ||
shape ............ The shape to draw. Either 'ellipse', 'arc', or 'rect' | ||
mode ............. The ellipseMode (for ellipse and arc), or rectMode (for rect) | ||
Either p5.CORNERS, p5.CORNER, p5.CENTER or p5.RADIUS | ||
x1, x2, x2, y2 ... Coordinates specifying the shape in CORNERS mode, | ||
i.e. (x1, y1) and (x2, y2) specify two opposite corners P1 and P2 | ||
*/ | ||
function shapeCorners(p5, shape, mode, x1, y1, x2, y2) { | ||
// Adjust coordinates for testing modes other than CORNERS | ||
if (mode === p5.CORNER) { | ||
// Find top left corner | ||
let x = p5.min(x1, x2); // x | ||
let y = p5.min(y1, y2); // y | ||
// Calculate width and height | ||
// Don't use abs(), so we get negative values as well | ||
let w = x2 - x1; // w | ||
let h = y2 - y1; // h | ||
x1 = x; y1 = y; x2 = w; y2 = h; | ||
} else if (mode === p5.CENTER) { | ||
// Find center | ||
let x = (x2 + x1) / 2; // x | ||
let y = (y2 + y1) / 2; // y | ||
// Calculate width and height | ||
// Don't use abs(), so we get negative values as well | ||
let w = x2 - x1; | ||
let h = y2 - y1; | ||
x1 = x; y1 = y; x2 = w; y2 = h; | ||
} else if (mode === p5.RADIUS) { | ||
// Find Center | ||
let x = (x2 + x1) / 2; // x | ||
let y = (y2 + y1) / 2; // y | ||
// Calculate radii | ||
// Don't use abs(), so we get negative values as well | ||
let r1 = (x2 - x1) / 2; // r1; | ||
let r2 = (y2 - y1) / 2; // r2 | ||
x1 = x; y1 = y; x2 = r1; y2 = r2; | ||
} | ||
|
||
if (shape === 'ellipse') { | ||
p5.ellipseMode(mode); | ||
p5.ellipse(x1, y1, x2, y2); | ||
} else if (shape === 'arc') { | ||
// Draw four arcs with gaps inbetween | ||
const GAP = p5.radians(30); | ||
p5.ellipseMode(mode); | ||
p5.arc(x1, y1, x2, y2, 0 + GAP, p5.HALF_PI - GAP); | ||
p5.arc(x1, y1, x2, y2, p5.HALF_PI + GAP, p5.PI - GAP); | ||
p5.arc(x1, y1, x2, y2, p5.PI + GAP, p5.PI + p5.HALF_PI - GAP); | ||
p5.arc(x1, y1, x2, y2, p5.PI + p5.HALF_PI + GAP, p5.TWO_PI - GAP); | ||
} else if (shape === 'rect') { | ||
p5.rectMode(mode); | ||
p5.rect(x1, y1, x2, y2); | ||
} | ||
} | ||
|
||
|
||
/* | ||
Comprehensive test for rendering ellipse(), arc(), and rect() | ||
with the different ellipseMode() / rectMode() values: CORNERS, CORNER, CENTER, RADIUS. | ||
Each of the 3 shapes is tested with each of the 4 possible modes, resulting in 12 test. | ||
Each test renders the shape in 16 different coordinate configurations, | ||
testing combinations of positive and negative coordinate values. | ||
*/ | ||
visualSuite('Shape Modes', function(...args) { | ||
// Shapes to test | ||
const SHAPES = [ 'ellipse', 'arc', 'rect' ]; | ||
|
||
// Modes to test (used with ellipseMode or rectMode, according to shape) | ||
const MODES = [ 'CORNERS', 'CORNER', 'CENTER', 'RADIUS' ]; | ||
|
||
for (let shape of SHAPES) { | ||
visualSuite(`Shape ${shape}`, function() { | ||
|
||
for (let mode of MODES) { | ||
visualTest(`Mode ${mode}`, function(p5, screenshot) { | ||
p5.createCanvas(240, 500); | ||
p5.translate(p5.width/2, p5.height/2); | ||
|
||
// Make the following calls to shapeCorners shorter | ||
// by omitting p5, shape and mode parameters | ||
function _shapeCorners(x1, y1, x2, y2) { | ||
shapeCorners(p5, shape, p5[mode], x1, y1, x2, y2); | ||
} | ||
|
||
// Quadrant I (Bottom Right) | ||
// P1 P2 | ||
_shapeCorners( 10, 10, 110, 60); // P1 Top Left, P2 Bottom Right | ||
_shapeCorners( 10, 120, 110, 70); // P1 Bottom Left, P2 Top Right | ||
_shapeCorners(110, 180, 10, 130); // P1 Bottom Right, P2 Top Left | ||
_shapeCorners(110, 190, 10, 240); // P1 Top Right, P2 Bottom Left | ||
|
||
// Quadrant II (Bottom Left) | ||
_shapeCorners(-110, 10, -10, 60); | ||
_shapeCorners(-110, 120, -10, 70); | ||
_shapeCorners(-10, 180, -110, 130); | ||
_shapeCorners(-10, 190, -110, 240); | ||
|
||
// Quadrant III (Top Left) | ||
_shapeCorners(-110, -240, -10, -190); | ||
_shapeCorners(-110, -130, -10, -180); | ||
_shapeCorners(-10, -70, -110, -120); | ||
_shapeCorners(-10, -60, -110, -10); | ||
|
||
// Quadrant IV (Top Right) | ||
_shapeCorners( 10, -240, 110, -190); | ||
_shapeCorners( 10, -130, 110, -180); | ||
_shapeCorners(110, -70, 10, -120); | ||
_shapeCorners(110, -60, 10, -10); | ||
|
||
screenshot(); | ||
}); // End of: visualTest | ||
} // End of: MODES loop | ||
|
||
}); // End of: Inner visualSuite | ||
} // End of: SHAPES loop | ||
}); // End of: Outer visualSuite |
Binary file added
BIN
+17.6 KB
test/unit/visual/screenshots/Shape Modes/Shape arc/Mode CENTER/000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
test/unit/visual/screenshots/Shape Modes/Shape arc/Mode CENTER/metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"numScreenshots": 1 | ||
} |
Binary file added
BIN
+17.6 KB
test/unit/visual/screenshots/Shape Modes/Shape arc/Mode CORNER/000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
test/unit/visual/screenshots/Shape Modes/Shape arc/Mode CORNER/metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"numScreenshots": 1 | ||
} |
Binary file added
BIN
+17.6 KB
test/unit/visual/screenshots/Shape Modes/Shape arc/Mode CORNERS/000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
test/unit/visual/screenshots/Shape Modes/Shape arc/Mode CORNERS/metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"numScreenshots": 1 | ||
} |
Binary file added
BIN
+17.6 KB
test/unit/visual/screenshots/Shape Modes/Shape arc/Mode RADIUS/000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
test/unit/visual/screenshots/Shape Modes/Shape arc/Mode RADIUS/metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"numScreenshots": 1 | ||
} |
Binary file added
BIN
+23.1 KB
test/unit/visual/screenshots/Shape Modes/Shape ellipse/Mode CENTER/000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
test/unit/visual/screenshots/Shape Modes/Shape ellipse/Mode CENTER/metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"numScreenshots": 1 | ||
} |
Binary file added
BIN
+23.1 KB
test/unit/visual/screenshots/Shape Modes/Shape ellipse/Mode CORNER/000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
test/unit/visual/screenshots/Shape Modes/Shape ellipse/Mode CORNER/metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"numScreenshots": 1 | ||
} |
Binary file added
BIN
+23.1 KB
test/unit/visual/screenshots/Shape Modes/Shape ellipse/Mode CORNERS/000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
test/unit/visual/screenshots/Shape Modes/Shape ellipse/Mode CORNERS/metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"numScreenshots": 1 | ||
} |
Binary file added
BIN
+23.1 KB
test/unit/visual/screenshots/Shape Modes/Shape ellipse/Mode RADIUS/000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
test/unit/visual/screenshots/Shape Modes/Shape ellipse/Mode RADIUS/metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"numScreenshots": 1 | ||
} |
Binary file added
BIN
+7.68 KB
test/unit/visual/screenshots/Shape Modes/Shape rect/Mode CENTER/000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
test/unit/visual/screenshots/Shape Modes/Shape rect/Mode CENTER/metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"numScreenshots": 1 | ||
} |
Binary file added
BIN
+7.68 KB
test/unit/visual/screenshots/Shape Modes/Shape rect/Mode CORNER/000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
test/unit/visual/screenshots/Shape Modes/Shape rect/Mode CORNER/metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"numScreenshots": 1 | ||
} |
Binary file added
BIN
+7.68 KB
test/unit/visual/screenshots/Shape Modes/Shape rect/Mode CORNERS/000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
test/unit/visual/screenshots/Shape Modes/Shape rect/Mode CORNERS/metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"numScreenshots": 1 | ||
} |
Binary file added
BIN
+7.68 KB
test/unit/visual/screenshots/Shape Modes/Shape rect/Mode RADIUS/000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
test/unit/visual/screenshots/Shape Modes/Shape rect/Mode RADIUS/metadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"numScreenshots": 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I believe creating a larger canvas might be an issue. When the tests run, a bigger canvas could make them take a bit longer due to its size. Plus, including files that are 15-20kB could make the codebase pretty heavy. Could we perhaps reduce it to 50x50? That would probably take up minimal space in bytes.
By doing this, we could include the tests one by one. Like, for each quadrant we can add one tests or something like that? For reference, you might want to check out this link: https://github.com/processing/p5.js/pull/6783/files#diff-aefa4b8e5287fbd0cdf37a6ec8d27322a4e5c893e99ec74d32053e60e2bdfd7d
Quadrant-1 -> all modes -> 1 particular tests? Or do you have any idea how could 50X50 size canvas could be fit for testing? I am also not 100% sure.
Maybe by using a 50x50 size, we could adjust the parameter values of _shapeCorners accordingly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in this case using many smaller canvases instead of a few bigger ones, will actually make things slower. Because then we'll have the overhead of createCanvas() i.e. creating a rendering context much more often, and the overall codebase size will also be larger with many small PNGs because of the PNG headers.
And to thoroughly test everything, we have 4 modes, times 3 shapes, times 4 quadrants, times 4 combinations of points, equals 192 shapes to draw. I see no way around that. Bear in mind this adds 192 test for fundamental drawing operations that were previously untested.
Therefore I propose making the canvas in the tests as small as possible, while still testing all the cases.
I have reduced the canvas size from 240x500 to 60x125 px, which reduces the total size of the generated PNGs from 223 to 12 kB.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good!