Lines


Defining a line object

A line can be generated in a number of ways. First, let’s look at a line defined by two points:

layout: OneGraph: graph: objects: # line defined by two points - Line: point: [3,4] point2: [6,5] # show points for reference - Point: coordinates: [3,4] - Point: coordinates: [6,5]

A line can also be created using a point and a slope:

layout: OneGraph: graph: objects: # line defined by a point and a slope - Line: point: [3,4] slope: -2 # show point for reference - Point: coordinates: [3,4]

Or by using the slope and the y-intercept:

layout: OneGraph: graph: objects: - Line: yIntercept: 2 slope: 2

Or by using the inverse slope and the x-intercept:

layout: OneGraph: graph: objects: - Line: xIntercept: 2 invSlope: 2

Or by using both intercepts:

layout: OneGraph: graph: objects: - Line: xIntercept: 2 yIntercept: 6

Finally, if you would like to create a line through the origin, you can provide just one point or a slope and the line will, by default, pass through the origin:

layout: OneGraph: graph: objects: # line defined only by a slope - Line: slope: 2 label: {text: "\\text{slope} = 2", x: 3} color: green # line defined only by a point - Line: point: [3,1] # show point for reference - Point: coordinates: [3,1]

Labeling a line

Lines are actually a special case of curves, so you can label lines just as you would a curve: in a label attribute, use the text attribute for the text and an x attribute describing the x-coordinate where you want the label to appear.

Just as with curves, you can also specify the range of values over which you would like the line evaluated.

The graph below shows two lines, both defined by the same two points. One is dotted and extends the full range of the graph; the other is solid, is limited to 0 < x < 8, and labeled at x = 5.

layout: OneGraph: graph: objects: # draw a dotted line (lineStyle = dotted) # defined by the points (3,4) and (7,8) - Line: lineStyle: dotted point: [3,4] point2: [7,8] color: blue # draw a thick line (strokewidth = 4) # defined by the same points, # but limited to the range x = 2 to x = 8 - Line: strokeWidth: 4 point: [3,4] point2: [7,8] min: 2 max: 8 color: blue label: text: y = x + 3 x: 5 # show points for reference - Point: coordinates: [3,4] - Point: coordinates: [7,8]

Calculations generated by a line

If you add a name attribute to a line, you can access calcs generated by the line. For example, consider a line determined by the points (5,1) and (4,2). There are a few things we might want to know about this line, as indicated by the three points in the graph below:

layout: OneGraph: graph: objects: - Line: name: myLine point: [1,6] point2: [3,2] label: x: 1 text: "`slope = ${calcs.myLine.slope}`" - Point: coordinates: [0, calcs.myLine.yIntercept] label: text: y \text{ intercept} - Point: coordinates: [calcs.myLine.xIntercept, 0] label: text: x \text{ intercept} - Point: coordinates: [calcs.myLine.fixedPoint, calcs.myLine.fixedPoint] - Line: slope: 1 strokeWidth: 0.75 color: grey - AngleMarker: measure: 45

Because the line is named myLine (line 6 of the code), it generates a calc object. This object has five attributes: xIntercept, yIntercept, slope, invSlope, and fixedPoint. You can then use these auto-generated calculations anywhere else in your graph: for example, to plot a point, or to add a text label.

The intercept and slope values are self-explanatory. The fixedPoint attribute corresponds to the point at which the graph crosses the 45-degree line.

KGJS
KineticGraphs.org
Getting Started

About the Project

Authoring Sandbox

Graphs & Layouts

Layouts

Sidebars

Graphs

Trees

3D Graphs

Graph Objects

Styling Graph Objects

Points

Lines

Curves

Contours

Shapes and Areas

Segments and Arrows

Angles and Angle Markers

User Interactions

Parameters

Calculations

Dragging

Economics

Econ Schema

Supply and Demand

Consumer Optimization

Exchange