Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Detecting whether two graphics are identical

Tags:

r

shapes

graphics

I am currently working on developing a package to generate visual aptitude assessment items in R. These items frequently start with a composition then have elements of the composition transformed in some way to create new compositions. What I need to know is that if there are alternative transformation which will lead to the same composition. Going through each composition potential options mathematically and seeing if the composition is invariant to certain transformations (circle cannot be rotated for example) or if different transformations lead to the same outcome (equilateral triangle rotated 180 is the same as flipped for example) seems too exhausting due the numerous potential combinations.

I am therefore wondering if there are any available available to check if two plots might be identical in R?

Say we had the two sets of commands which both produce squares:

plot(c(0,1), c(0,1), type='n')
  lines(c(.15,.85,.85,.15,.15),c(.15,.15,.85,.85,.15))

plot(c(0,1), c(0,1), type='n')
  rect(.15,.15,.85,.85)

Is there any tools available for a pixel by pixel comparison of the two graphical renderings?

like image 270
Francis Smart Avatar asked Sep 30 '22 03:09

Francis Smart


1 Answers

You can save your pictures as png and load them and compare the respective rasters. Comparing rasters is just comparing pixel matrix. For example doing this with you example:

png(filename="pic1.png")
plot(c(0,1), c(0,1), type='n')
lines(c(.15,.85,.85,.15,.15),c(.15,.15,.85,.85,.15))
dev.off()

png(filename="pic2.png")
plot(c(0,1), c(0,1), type='n')
rect(.15,.15,.85,.85)
dev.off()

Then comparing rasters we find that they are identical:

library(png)
pic1 = as.raster(readPNG("pic1.png"))
pic2 = as.raster(readPNG("pic2.png"))

identical(pic1,pic2)
[1] TRUE
like image 117
agstudy Avatar answered Oct 19 '22 17:10

agstudy