Skip to content

Commit

Permalink
v1.3 add shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
asjadnaqvi committed Oct 11, 2024
1 parent 8345ebe commit 848a180
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 63 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@



# graphfunctions v1.2
*(08 Oct 2024)*
# graphfunctions v1.3
*(11 Oct 2024)*

A suite of programs to help enhance figures in Stata. The program is designed to be called by other programs, but it can be used as a standalone as well. The page will provide some minimum examples, but for the full scope of each program, see the relevant help files.

Expand All @@ -19,8 +19,8 @@ Currently, this package contains:
|----| ---- | ---- | ----- |
| [labsplit](#labsplit) | 1.1 | 28 Sep 2024 | Text wrapping |
| [catspline](#catspline) | 1.0 | 04 Oct 2024 | Catmull-Rom splines |
| [arc](#arc) | 1.0 | 08 Oct 2024 | Arcs between two points |

| [arc](#arc) | 1.1 | 11 Oct 2024 | Arcs between two points |
| [shapes](#shapes) | 1.0 | 11 Oct 2024 | Draw shapes |


The programs here are designed/upgraded/bug-fixed on a needs basis, mostly to support other packages. If you have specific requests, or find major bugs, then please open an [issue](https://github.com/asjadnaqvi/stata-graphfunctions/issues).
Expand All @@ -35,7 +35,7 @@ SSC (**v1.0**):
ssc install graphfunctions, replace
```

GitHub (**v1.2**):
GitHub (**v1.3**):

```stata
net install graphfunctions, from("https://raw.githubusercontent.com/asjadnaqvi/stata-graphfunctions/main/installation/") replace
Expand Down Expand Up @@ -147,7 +147,7 @@ twoway ///


### arc
*(v1.0: 08 Oct 2024)*
*(v1.1: 11 Oct 2024)*

Draw minor or major arcs between two points. The arc orientation and be switched using `swap`, and major arcs can be drawn using `major`.

Expand Down Expand Up @@ -210,13 +210,22 @@ twoway ///
<img src="/figures/arc4.png" width="75%">


### shapes
*(v1.0: 11 Oct 2024)*



## Feedback

Please open an [issue](https://github.com/asjadnaqvi/stata-graphfunctions/issues) to report errors, feature enhancements, and/or other requests.


## Change log

**v1.3 (11 Oct 2024)**
- `shapes` added.
- `arc` bug fixes plus code cleanup.

**v1.2 (08 Oct 2024)**
- `arc` added.
- Bug fixes in `labsplit`.
Expand Down
65 changes: 23 additions & 42 deletions installation/arc.ado
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*! arc v1.0 (08 Oct 2024)
*! arc v1.1 (11 Oct 2024)
*! Asjad Naqvi (asjadnaqvi@gmail.com)

* v1.1 (11 Oct 2024): Minor bug fixes.
* v1.0 (08 Oct 2024): first release.


Expand All @@ -13,7 +14,7 @@ version 11
syntax, ///
x1(numlist max=1) y1(numlist max=1) /// // from
x2(numlist max=1) y2(numlist max=1) /// // to
[ n(real 80) RADius(numlist max=1 >0) major swap ] ///
[ n(real 40) RADius(numlist max=1 >0) major swap ] ///
[ genx(string) geny(string) replace ]


Expand All @@ -27,7 +28,6 @@ version 11
local xm = (`x1' + `x2') / 2
local ym = (`y1' + `y2') / 2


// slope
local dx = `x2' - `x1'
local dy = `y2' - `y1'
Expand All @@ -40,8 +40,7 @@ version 11
display in yellow "Radius of `radius' assumed."
}


// Check that radius is valid
// validate the radius
if `radius' <= `L'/2 {
local mychord = `L' / 2
di as error "Radius must be greater than half the chord length. Half chord length for given coordinates = `mychord'"
Expand All @@ -51,39 +50,27 @@ version 11
return local chord `L'
return local radius `radius'

// Distance from midpoint to center
// distance from midpoint to center
local h_dist = sqrt(`radius'^2 - (`L'/2)^2)


// Determine the direction of the perpendicular bisector
// direction of the perpendicular bisector
local ux_perp = -`dy' / `L'
local uy_perp = `dx' / `L'

local cross_prod = `dx' * `uy_perp' - `dy' * `ux_perp'

// switch from left to right orientation
// swap left to right orientation
if "`swap'" == "" {
if `cross_prod' > 0 {
local h = `xm' + `h_dist' * `ux_perp'
local k = `ym' + `h_dist' * `uy_perp'
}
else {
local h = `xm' - `h_dist' * `ux_perp'
local k = `ym' - `h_dist' * `uy_perp'
}
}
local h = `xm' + `h_dist' * `ux_perp'
local k = `ym' + `h_dist' * `uy_perp'
}
else {
if `cross_prod' < 0 {
local h = `xm' + `h_dist' * `ux_perp'
local k = `ym' + `h_dist' * `uy_perp'
}
else {
local h = `xm' - `h_dist' * `ux_perp'
local k = `ym' - `h_dist' * `uy_perp'
}
local h = `xm' - `h_dist' * `ux_perp'
local k = `ym' - `h_dist' * `uy_perp'
}



// get the angles in order
local angle_start = atan2(`y1' - `k', `x1' - `h')
local angle_end = atan2(`y2' - `k', `x2' - `h')
Expand All @@ -93,53 +80,47 @@ version 11
if `angle_end' < 0 local angle_end = `angle_end' + 2 * _pi



// minor (smaller arc) vs major (larger arc)
// correct for minor vs major arc

if "`swap'"!= "" {
if "`major'" != "" {
if (`angle_end' - `angle_start') < _pi local angle_end = `angle_end' + 2 * _pi
if (`angle_end' - `angle_start') < _pi local angle_end = `angle_end' + 2 * _pi
}
else {
if (`angle_end' - `angle_start') > _pi local angle_end = `angle_end' - 2 * _pi
if (`angle_end' - `angle_start') > _pi local angle_end = `angle_end' - 2 * _pi
}
}
else {
if "`major'" != "" {
if (`angle_end' - `angle_start') > _pi local angle_end = `angle_end' - 2 * _pi
if (`angle_end' - `angle_start') > 2 * _pi local angle_end = `angle_end' - 2 * _pi

if (`angle_end' - `angle_start') > _pi local angle_end = `angle_end' - 2 * _pi
}
else {
if (`angle_end' - `angle_start') < _pi local angle_end = `angle_end' + 2 * _pi
if (`angle_end' - `angle_start') > 2 * _pi local angle_end = `angle_end' - 2 * _pi
if (`angle_end' - `angle_start') < _pi local angle_end = `angle_end' + 2 * _pi
}
if (`angle_end' - `angle_start') > 2 * _pi local angle_end = `angle_end' - 2 * _pi
}


// angle segments
local delta_theta = (`angle_end' - `angle_start') / (`n' - 1)

if _N < `n' set obs `n'

tempvar theta
gen double `theta' = `angle_start' + (_n - 1) * `delta_theta'

if _N < `n' set obs `n'
local xvar _x
local yvar _y


if "`genx'" != "" local xvar `genx'
if "`geny'" != "" local yvar `geny'

if "`replace'" != "" {
capture drop `xvar'
capture drop `yvar'
}

gen double `xvar' = `h' + `radius' * cos(`theta')
gen double `yvar' = `k' + `radius' * sin(`theta')



return local xcirc = `h'
return local ycirc = `k'

Expand Down
6 changes: 3 additions & 3 deletions installation/arc.sthlp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{smcl}
{* 08Oct2024}{...}
{* 11Oct2024}{...}
{hi:help arc}{...}
{right:{browse "https://github.com/asjadnaqvi/stata-graphfunctions":graphfunctions (GitHub)}}

Expand Down Expand Up @@ -53,8 +53,8 @@ More examples on {browse "https://github.com/asjadnaqvi/stata-graphfunctions":Gi

{title:Package details}

Version : {bf:arc} v1.0 in {stata help graphfunctions:graphfunctions}
This release : 08 Oct 2024
Version : {bf:arc} v1.1 in {stata help graphfunctions:graphfunctions}
This release : 11 Oct 2024
First release: 08 Oct 2024
Repository : {browse "https://github.com/asjadnaqvi/stata-graphfunctions":GitHub}
Keywords : Stata, graph, arcs
Expand Down
9 changes: 6 additions & 3 deletions installation/graphfunctions.pkg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
v 1.2
v 1.3
d {bf:GRAPHFUNCTIONS}: A collection of functions to extend graph functionality in Stata
d
d Requires: Stata version 11 or higher.
Expand All @@ -8,8 +8,9 @@ d KW: graphs
d KW: figures
d KW: text wrap
d KW: splines
d KW: shapes
d
d Distribution-Date: 20241008
d Distribution-Date: 20241011
d License: MIT
d
d Author: Asjad Naqvi (asjadnaqvi@gmail.com)
Expand All @@ -20,4 +21,6 @@ f labsplit.sthlp
f catspline.ado
f catspline.sthlp
f arc.ado
f arc.sthlp
f arc.sthlp
f shapes.ado
f shapes.sthlp
19 changes: 10 additions & 9 deletions installation/graphfunctions.sthlp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{smcl}
{* 08Oct2024}{...}
{* 11Oct2024}{...}
{hi:help graphfunctions}{...}
{right:{browse "https://github.com/asjadnaqvi/stata-graphfunctions":graphfunctions v1.2 (GitHub)}}
{right:{browse "https://github.com/asjadnaqvi/stata-graphfunctions":graphfunctions v1.3 (GitHub)}}


{title:GRAPH FUNCTIONS}:
Expand All @@ -12,8 +12,9 @@ This package contains the following program(s):
{hline}

{stata help labsplit:labsplit} Various options for wrapping labels.
{stata help labsplit:catspline} Generate Catmull-Rom splines.
{stata help labsplit:arc} Draw an arc between two points.
{stata help catspline:catspline} Generate Catmull-Rom splines.
{stata help arc:arc} Draw an arc between two points.
{stata help shapes:shapes} Draw shapes.

{hline}

Expand All @@ -32,21 +33,21 @@ Please submit bugs, errors, feature requests on {browse "https://github.com/asja

Suggested citation guidlines for this package:

Naqvi, A. (2024). Stata package "graphfunctions" version 1.2. Release date 08 October 2024. https://github.com/asjadnaqvi/stata-graphfunctions.
Naqvi, A. (2024). Stata package "graphfunctions" version 1.3. Release date 11 October 2024. https://github.com/asjadnaqvi/stata-graphfunctions.

@software{graphfunctions,
author = {Naqvi, Asjad},
title = {Stata package ``graphfunctions''},
url = {https://github.com/asjadnaqvi/stata-graphfunctions},
version = {1.2},
date = {2024-10-08}
version = {1.3},
date = {2024-10-11}
}


{title:Package details}

Version : {bf:graphfunctions} v1.2
This release : 08 Oct 2024
Version : {bf:graphfunctions} v1.3
This release : 11 Oct 2024
First release: 28 Sep 2024
Repository : {browse "https://github.com/asjadnaqvi/stata-graphfunctions":GitHub}
License : {browse "https://opensource.org/licenses/MIT":MIT}
Expand Down
56 changes: 56 additions & 0 deletions installation/shapes.ado
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
*! shapes v1.0 (11 Oct 2024)
*! Asjad Naqvi (asjadnaqvi@gmail.com)

* v1.0 (11 Oct 2024): first release.


cap program drop shapes

program define shapes

version 11
gettoken left 0: 0, parse(", ")

if "`left'" == "circle" {
_circle `0'
}

end



program define _circle
version 11

syntax [, n(real 6) ROtate(real 0) RADius(real 10) ]


quietly {
if _N < `n' set obs `n'

local rotate = `rotate' * _pi / 180

tempvar _seq _angle

gen `_seq' = _n
gen double `_angle' = (`_seq' * 2 * _pi / `n') + `rotate'

gen double _x = `radius' * cos(`_angle')
gen double _y = `radius' * sin(`_angle')


expand 2 if `_seq'== 1
set obs `=_N+1' // pad a row in case more rows are added.
gen _id = _n

order _id
}


end






Loading

0 comments on commit 848a180

Please sign in to comment.