Categories
Python Programming

Arithmetic Operators

There are seven basic arithmetic operators in the Python programming language.

They are:

  1. Addition
  2. Subtraction
  3. Multiplication
  4. Division
  5. Integer Division
  6. Exponentiation
  7. Modulus

Addition

This is represented by the + symbol. It adds two numbers together and gives their sum.

Subtraction

This is represented by the – operator. It takes the value on the right from the value on the left to give the difference ot the two values.

Multiplication

This is represented by the * operator. It multiplies the value on the left with the value on the right to give a product of the two.

Division

This is represented by the / operator. It divides the value on the left with the value on the right to give the quotient.

Integer Division

This is a special form of division represented by //. It gives the whole number value of the quotient.

Exponentiation

This is represented by the ** operator. It gives the result of a value raised to another value.

Modulus

This is the remainder of division of one number by another number. It is used to test if the number on the right is a factor of the number of the left.

Conclusion

The above operators are how arithmetic is done in the Python programming language on a basic level.

Categories
Vector Graphics for the Web

Vector Graphics with RaphaelJS

RaphaelJS is a JavaScript library that provides an API for manipulating SVG, and SVG support for Internet Explorer. It achieves the latter by emulating SVG in Internet Explorer using VML.

SVG is a language for describing vector graphics in XML. SVG is a W3C specification and works well with HTML, CSS and JavaScript.

Setup

RaphaelJS is a JavaScript library so setting it up requires simply connecting to it in your code by linking to it.

The code below will draw a circle of radius 50 pixels at point (50, 50).

<html>
	<head>
		<title>Circle</title>
	</head>
	<body>
		<div id="container"></div>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
		<script>
			paper = Raphael('container', 100, 100);
			var circle = paper.circle(50, 50, 25);
		</script>
	</body>
</html>

<div id=”container”> contains a div element that the library will be drawing in.

paper = Raphael(‘container’, 100, 100); creates a canvas object on which to draw. You must do this first, as all future calls to drawing methods from this instance will be bound to this canvas.

The first argument in the function Raphael() is the id of the HTML element inside of which you would like to start drawing things.

var circle = paper.circle(50, 50, 25); creates a RaphaelJS SVG object inside the thing with id=”container” that is a circle which is 50 pixels from the top and left of the paper.

RaphaelJS Elements

RaphaelJS supports 3 kinds of elements: shapes, images and text. This article will cover how to draw shapes in RaphaelJS.

After you create your paper object, in order to work with Raphael elements, you must:

  1. Create a RaphaelJS element
  2. Manipulate the style of the element
  3. Add events to the element using JavaScript

Drawing a Circle

The code to draw a circle from the RaphaelJS documentation is Paper.circle(x, y, r) where x is the x coordinate of the circle, y is the y coordinate of the circle and r is the radius.

The code in the Setup section above already shows how to draw a circle of radius 25 at the point 50, 50 of the paper.

To add attributes to the circle is trivial. To draw a red circle with a black stroke, we use the code shown below:

<script>
	paper = Raphael('container', 100, 100);
	var circle = paper.circle(50, 50, 25);
	var attributes = {
			fill: "#FF0000",
			stroke: '#000',
			"stroke-width": 3,
			"stroke-linejoin": "round",
		};
	circle.attr(attributes);
</script>

The generated image is shown below:

Circle
Circle with a Black Border

Drawing an Ellipse

The code to draw an ellipse from the RaphaelJS documentation is Paper.ellipse(x, y, rx, ry) where x is the x coordinate of the centre, y is the y coordinate of the centre, rx is the horizontal radius and ry is the vertical radius.

The code to draw an ellipse is shown below:

<script>
	paper = Raphael('container', 100, 100);
	var ellipse = paper.ellipse(50, 50, 40, 20);
	var attributes = {
			fill: "#FF0000",
			stroke: '#000',
			"stroke-width": 3,
			"stroke-linejoin": "round",
		};
	ellipse.attr(attributes);
</script>

The generated ellipse is shown below:

Ellipse
Ellipse with a Black Border

Drawing a Rectangle

The code to draw a rectangle from the RaphaelJS documentation is Paper.rect(x, y, width, height, [r]) where x is the x coordinate of the top left corner, y is the y coordinate of the top left corner, width is the width of the rectangle and height is the height of the rectangle.

[r] is optional and is for the radius of the rectangle if it would have rounded corners.

<script>
	paper = Raphael('container', 100, 100);
	var rectangle = paper.rect(10, 10, 50, 50);
	var attributes = {
			fill: "#FF0000",
			stroke: '#000',
			"stroke-width": 3,
			"stroke-linejoin": "round",
		};
	rectangle.attr(attributes);
</script>

The generated rectangle is shown below:

Rectangle
Rectangle with a Black Border

Conclusion

The RaphaelJS library is a very versatile library for drawing in the browser. Since its’ release in 2008, it has gone on to become a simple library for creating vector graphics in the browser.

Categories
Vector Graphics for the Web

Vector Graphics for the Web – Introduction

When working on the web, there are two types of image formats that you can use:

  1. Raster Graphics: They are made up of pixels. The more pixels you have the higher the resolution you get.
  2. Vector Graphics: They use mathematical equations to create a two dimensional image. The graphics data is described by mathematical formula.

This article will look at vector graphics and how to work with them using JavaScript.

Vector graphics can be drawn by writing code to create them. Programs such as Inkscape and Illustrator also use them in their default formats.

Take for example the map of Africa shown above. It is a composite shape made up of individual paths that each represents a country. With this knowledge, we can apply a transformation to the individual countries to any size. Provided we do the transformations uniformly, we can size or resize the image without distortion.

The SVG Specification

SVG is a language for describing vector graphics in XML. SVG is a W3C specification and works well with HTML, CSS and JavaScript.

Below is a basic skeleton of an SVG document. xlmns specifies the namespace for the document.

<svg xmlns="http://www.w3.org/2000/svg"> 
 
</svg>

To draw a circle, we specify the properties of the circle in the document. So our document becomes:

<svg xmlns="http://www.w3.org/2000/svg"> 
	<circle cx="40" cy="40" r="30" stroke="#000" stroke-width="1" fill="#FFF" />
</svg>

Saving the above in a circle.svg document and viewing in a browser will draw the circle shown below:

Circle in SVG

SVG Advantages

Resolution Independence

Vector Graphics are Resolution Independent
Vector Graphics are Resolution Independent

Scalable Vector Graphics (SVG) do not lose their resolution regardless of how they are resized.

Interactivity

You can add hyperlinks and animation to SVG by styling and scripting. SVG has a navigable DOM which allows for the creation of interactive elements.

SEO Friendly

SVG is made up of XML text files. As a result, you can add keyword descriptions and links making content more recognisable for search engines.

Adding SVG to Your Web Pages

Using the Image Tag

An SVG document can be embedded in the image tag like a regular image in HTML. An example is shown below:

<img src="circle.svg" alt="A simple circle" width="100" height="100" />

The disadvantage of this method is that the SVG will not be interactive.

Using the Object Tag

This is the best option if you intend to make the SVG interactive. The code to do so is shown below:

<object type="image/svg+xml" data="circle.svg"></object>

Using inline SVG

In this method, the code to generate the svg is placed between the body tags of the web pages.

Browser Adoption

Browser Support for SVG
Browser Support for SVG

On the Can I Use website, most modern browsers have great support for SVG with the exception of Internet Explorer.

JavaScript Tools for Working with SVG

RaphaelJS

RaphaelJS is a JavaScript library that allows you to work with vector graphics using JavaScript. It acts like an interface for creating SVG without having to write code in SVG.

Think of it like jQuery. Without a wrapper like jQuery providing a layer of abstraction for JavaScript, you would have to write raw JavaScript to do anything on the web. If you think of how tedious writing JavaScript is and multiply that by 10, you will have an idea of the difficulty of SVG.

RaphaelJS is a vector graphics library which is used to draw objects in the browser. It enables SVG support for IE8 and below.

JustGage

JustGage is a handy JavaScript plugin for generating and animating nice & clean gauges. It is based on RaphaelJS library for vector drawing, so it’s completely resolution independent and self-adjusting.

Oh yes, since it’s pure SVG, it works in almost any browser –
IE6+, Chrome, Firefox, Safari, Opera, Android, etc.

Snap.svg

Snap.svg was developed by the creator of the RaphaelJS library. Unlike RaphaelJS which supports IE8 and below, Snap.svg was designed for modern browsers and therefore supports the newest SVG features like masking, clipping, patterns, full gradients, groups, and more.

D3.js

D3.js is a JavaScript library for manipulating documents based on data. D3helps you bring data to life using HTML, SVG, and CSS. D3’s emphasis on web standards gives you the full capabilities of modern browsers without tying yourself to a proprietary framework, combining powerful visualization components and a data-driven approach to DOM manipulation.

Conclusion

In the world of front end web development, there are a plethora of tools that are used to create interactive web applications. This tools involve working with HTML5, CSS3, SVG and WebGL.

The main advantage of SVG is that it allows you create graphics without the loss of resolution when they are resized. This might not seem like much to you but in a world of responsive design, it represents a big opportunity.

Categories
Make Money Online

Medium Partner Program Earnings Report – August 2020

Above is my earnings report for the Medium Partner Program in August. I wrote my first for a publication on August 28 and 3 days later I have made over 1 dollar.

Writing is one of the ways to make money online. The hard work with the Medium Partner Program is the generation of unique content where you are a subject matter expert.

I expect to continue writing in September. I will also give a report then. Have a great new month.

Categories
Make Money Online

Medium Partner Program Nigeria

On the 13th of August, I would sign up for the Medium Partner Program. I had heard about the Medium Partner Program but had been unable to join because payouts for the Medium Partner Program are made through Stripe. Sadly, Nigeria is not on the list of countries supported by Stripe.

Reading this article where the author explained how to sign up from Nigeria would guide me through the steps needed to join the Medium Partner Program from Nigeria.

If you are a writer based in Nigeria, this article will explain how you can benefit from the Medium Partner Program. It is one of the way to make money online in Nigeria.

On the 28th of August, I would submit my first article to a publication and the rest would be history.

You can read my article using my friend link. So although the article is behind a paywall, you don’t have to pay anything to read it.