daily pastebin goal
8%
SHARE
TWEET

Example 1

a guest Jun 5th, 2018 171 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use v6.c;
  2.  
  3. class Point2D {
  4.     has Numeric $.abscissa;
  5.     has Numeric $.ordinate;
  6.  
  7.     method coordinates {
  8.         # accessor to both x and y
  9.         return (self.abscissa, self.ordinate)
  10.     }
  11.    
  12.     method distance2center {
  13.         (self.abscissa ** 2 + self.ordinate ** 2) ** 0.5
  14.     }
  15.  
  16.     method polar-coordinates {
  17.         my $radius = self.distance2center;
  18.         my $theta = atan2 self.ordinate, self.abscissa;
  19.     return $radius, $theta;
  20.     }
  21.  
  22. }
  23.  
  24. class Pixel is Point2D {
  25.     has %.color is rw;
  26.  
  27.     method change_color(%hue) {
  28.         %!color = %hue
  29.     }
  30.  
  31.     method change_color2(Int $red, Int $green, Int $blue) {
  32.         # signature using positional parameters
  33.         self.color = (red => $red, green => $green, blue => $blue)
  34.     }
  35. }
  36.  
  37. my $pix = Pixel.new(
  38.     :abscissa(3.3),
  39.     :ordinate(4.2),
  40.     color => {red => 34, green => 233, blue => 145},
  41. );
  42.  
  43. say "Original colors: ", $pix.color;
  44. $pix.change_color({:red(195), :green(110), :blue(70),});
  45. say "Modified colors: ", $pix.color;
  46. say "New pixel caracteristics:";
  47. printf "\tAbscissa: %.2f\n\tOrdinate: %.2f\n\tColors: R: %d, G: %d, B: %d\n",
  48. $pix.abscissa, $pix.ordinate,
  49. $pix.color<red>, $pix.color{"green"}, $pix.color{"blue"};
  50. $pix.change_color2(90, 180, 30); # positional args
  51. say "New colors:
  52. \tR: {$pix.color<red>}, G: {$pix.color<green>}, B: {$pix.color<blue>} ";
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top