Introduction to Programming for College Students: How to Draw Basic Rectangle

http://gurakura.sakura.ne.jp/hellomondrian/rect1/

Introduction: Draw a rectangle

composition-in-color-a-1917.jpg

The image above is Piet Mondrian's work Composition in Color A (1917), [https://www.wikiart.org/en/piet-mondrian/composition-in-color-a-1917](https: // www.wikiart.org/en/piet-mondrian/composition-in-color-a-1917) You can see it at.

I explained how to draw a straight line in Previous Chapter. If you just want to outline a rectangle, you can call the line function four times.

In this chapter, we will explain how to draw a rectangle that fills the inside, that is, a rectangle as an area, instead of such a drawing method.

How to draw a basic rectangle

Use the rect function to draw a rectangle whose horizontal and vertical lines are parallel to the window. The arguments of the rect function are rect (x, y, w, h), respectively.

x = x coordinate of the upper left vertex of the rectangle y = y coordinate of the upper left vertex of the rectangle w = Width of rectangle h = height of rectangle

It will be.

The drawing of the rectangle by rect is affected by the line width setting by strokeWeight and the line color by stroke.

background(250,250,250);
size(500,500);

strokeWeight(10);  // line width: 10 pixels.
stroke(0,64,255);  // line color: light blue.
rect(100,150,200,100);

First Rect

The drawing color inside the rectangle can be specified with the fill function. This function has various uses like the stroke function, but here we will introduce how to specify RGB values, fill (r, g, b).

In the program list shown below, the outline is blue as in the previous program, and the inside is specified to be filled with red.

background(250,250,250);
size(500,500);

strokeWeight(10);  // line width: 10 pixels.
stroke(0,64,255);  // line color: light blue.

fill(255,0,0);	   // <--- fill color: red.

rect(100,150,200,100);

Rect2

As you can see, when drawing a rectangle, you have to manage the outline color and the two colors that fill the inside.

You may find it annoying, but programming is a series of such tasks. Well, it's a simple, detailed, and tedious task, isn't it? So please be kind to programmers (no, really ...).

At the end of this section, I'd like to consider how to avoid contouring. What do you think you should do? I think there are various ideas.

Should I instruct Processing not to draw contours in the first place? Some people thought that it would disappear if the width of the outline was set to 0. Etc., etc. Both of these opinions are correct.

As you can see, there are multiple ways to achieve the same effect in programming.

Of course, in some cases the execution speed may be different, and it may not be exactly the same.

However, in real-world programming, there are often different ways to reach the same goal.

There is a noStroke function as a function to turn off contour lines. Since this is a function that requires no arguments, it is used as stroke (); in a real program. To enable the disabled contour line, specify the contour line color with the stoke function.

When the contour is turned off with the noStroke function, the information about the width of the contour remains, so the contour of the rectangle drawn by the second rect function remains at 10 pixels.

background(250,250,250);
size(500,500);

strokeWeight(10);  // line width: 10 pixels.
stroke(0,64,255);  // set color, but disabled by noStroke
fill(255,0,0);
noStroke();
rect(100,150,200,100);

stroke(0,64,255);  // stroke ON (line color: light blue.)
rect(100,400,50,80);

noStroke rect

Recommended Posts

Introduction to Programming for College Students: How to Draw Basic Rectangle
Introduction to Programming for College Students: Introduction
Introduction to Programming for College Students: Variables
Introduction to Programming for College Students: Draw a Straight Line
Introduction to Programming for College Students: Various Functions Related to Rectangle (Part 2)
Introduction to Programming for College Students: Various Functions Related to Rectangle (Part 1)
Introduction to programming for college students (updated from time to time)
Introduction to Programming for College Students: Making a Canvas
Introduction to Programming for College Students: Preparation Let's Install Processing
Introduction to Programming for College Students: Make Straight Lines More Attractive
How to build Docker + Springboot app (for basic learning)
Introduction to Java for beginners Basic knowledge of Java language ①
How to introduce Basic authentication
An introduction to functional programming for object-oriented programmers in Elm
How to specify validation for time_field
How to install JMeter for Mac
How to master programming in 3 months
Introduction to Functional Programming (Java, Javascript)
[Introduction to Rails] How to use render
How to use binding.pry for view files
How to install Play Framework 2.6 for Mac
Introduction to java for the first time # 2
Introduction to Ruby basic grammar with yakiniku
[Introduction to Java] How to write a Java program
How to create a Maven repository for 2020
[Ruby] How to use slice for beginners
Introduction to kotlin for iOS developers ⑥-Kotlin creation
Introduction to kotlin for iOS developers ④-Type
Needed for iOS 14? How to set NSUserTrackingUsageDescription
Summarized how to climb the programming stairs
[For beginners] How to debug in Eclipse
[For beginners] Introduction to Java Basic knowledge of Java language ③ Array, selection structure, iteration structure